htc_send.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570
  1. /*
  2. * Copyright (c) 2013-2020 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "htc_debug.h"
  19. #include "htc_internal.h"
  20. #include "htc_credit_history.h"
  21. #include <qdf_mem.h> /* qdf_mem_malloc */
  22. #include <qdf_nbuf.h> /* qdf_nbuf_t */
  23. #include "qdf_module.h"
  24. /* #define USB_HIF_SINGLE_PIPE_DATA_SCHED */
  25. /* #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED */
  26. #define DATA_EP_SIZE 4
  27. /* #endif */
  28. #define HTC_DATA_RESOURCE_THRS 256
  29. #define HTC_DATA_MINDESC_PERPACKET 2
  30. enum HTC_SEND_QUEUE_RESULT {
  31. HTC_SEND_QUEUE_OK = 0, /* packet was queued */
  32. HTC_SEND_QUEUE_DROP = 1, /* this packet should be dropped */
  33. };
  34. #ifndef DEBUG_CREDIT
  35. #define DEBUG_CREDIT 0
  36. #endif
  37. #if DEBUG_CREDIT
  38. /* bit mask to enable debug certain endpoint */
  39. static unsigned int ep_debug_mask =
  40. (1 << ENDPOINT_0) | (1 << ENDPOINT_1) | (1 << ENDPOINT_2);
  41. #endif
  42. #ifdef QCA_WIFI_NAPIER_EMULATION
  43. #define HTC_EMULATION_DELAY_IN_MS 20
  44. /**
  45. * htc_add_delay(): Adds a delay in before proceeding, only for emulation
  46. *
  47. * Return: None
  48. */
  49. static inline void htc_add_emulation_delay(void)
  50. {
  51. qdf_mdelay(HTC_EMULATION_DELAY_IN_MS);
  52. }
  53. #else
  54. static inline void htc_add_emulation_delay(void)
  55. {
  56. }
  57. #endif
  58. void htc_dump_counter_info(HTC_HANDLE HTCHandle)
  59. {
  60. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  61. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  62. ("\n%s: ce_send_cnt = %d, TX_comp_cnt = %d\n",
  63. __func__, target->ce_send_cnt, target->TX_comp_cnt));
  64. }
  65. int htc_get_tx_queue_depth(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id)
  66. {
  67. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  68. HTC_ENDPOINT *endpoint = &target->endpoint[endpoint_id];
  69. return HTC_PACKET_QUEUE_DEPTH(&endpoint->TxQueue);
  70. }
  71. qdf_export_symbol(htc_get_tx_queue_depth);
  72. void htc_get_control_endpoint_tx_host_credits(HTC_HANDLE HTCHandle,
  73. int *credits)
  74. {
  75. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  76. HTC_ENDPOINT *pEndpoint;
  77. int i;
  78. if (!credits || !target) {
  79. AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: invalid args", __func__));
  80. return;
  81. }
  82. *credits = 0;
  83. LOCK_HTC_TX(target);
  84. for (i = 0; i < ENDPOINT_MAX; i++) {
  85. pEndpoint = &target->endpoint[i];
  86. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  87. *credits = pEndpoint->TxCredits;
  88. break;
  89. }
  90. }
  91. UNLOCK_HTC_TX(target);
  92. }
  93. static inline void restore_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
  94. {
  95. qdf_nbuf_t netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  96. if (pPacket->PktInfo.AsTx.Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) {
  97. qdf_nbuf_unmap(target->osdev, netbuf, QDF_DMA_TO_DEVICE);
  98. pPacket->PktInfo.AsTx.Flags &= ~HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  99. }
  100. if (pPacket->PktInfo.AsTx.Flags &
  101. HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA) {
  102. qdf_nbuf_pull_head(netbuf, sizeof(HTC_FRAME_HDR));
  103. }
  104. }
  105. static void send_packet_completion(HTC_TARGET *target, HTC_PACKET *pPacket)
  106. {
  107. HTC_ENDPOINT *pEndpoint = &target->endpoint[pPacket->Endpoint];
  108. HTC_EP_SEND_PKT_COMPLETE EpTxComplete;
  109. restore_tx_packet(target, pPacket);
  110. /*
  111. * In case of SSR, we cannot call the upper layer completion
  112. * callbacks, hence just free the nbuf and HTC packet here.
  113. */
  114. if (target->hif_dev && hif_get_target_status(target->hif_dev)) {
  115. htc_free_control_tx_packet(target, pPacket);
  116. return;
  117. }
  118. /* do completion */
  119. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  120. ("HTC calling ep %d send complete callback on packet %pK\n",
  121. pEndpoint->Id, pPacket));
  122. EpTxComplete = pEndpoint->EpCallBacks.EpTxComplete;
  123. if (EpTxComplete)
  124. EpTxComplete(pEndpoint->EpCallBacks.pContext, pPacket);
  125. else
  126. qdf_nbuf_free(pPacket->pPktContext);
  127. }
  128. #ifdef FEATURE_RUNTIME_PM
  129. /**
  130. * log_packet_info() - Log HTC packet information
  131. *
  132. * @target: handle of HTC context
  133. * @pPacket: handle of HTC packet
  134. *
  135. * Return: None
  136. */
  137. static void log_packet_info(HTC_TARGET *target, HTC_PACKET *pPacket)
  138. {
  139. HTC_ENDPOINT *pEndpoint = &target->endpoint[pPacket->Endpoint];
  140. HTC_EP_LOG_PKT ep_log_pkt;
  141. qdf_nbuf_t netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  142. ep_log_pkt = pEndpoint->EpCallBacks.ep_log_pkt;
  143. if (ep_log_pkt) {
  144. qdf_nbuf_pull_head(netbuf, sizeof(HTC_FRAME_HDR));
  145. ep_log_pkt(pEndpoint->EpCallBacks.pContext, pPacket);
  146. qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
  147. }
  148. }
  149. #else
  150. static void log_packet_info(HTC_TARGET *target, HTC_PACKET *pPacket)
  151. {
  152. }
  153. #endif
  154. void htc_send_complete_check_cleanup(void *context)
  155. {
  156. HTC_ENDPOINT *pEndpoint = (HTC_ENDPOINT *) context;
  157. htc_send_complete_check(pEndpoint, 1);
  158. }
  159. HTC_PACKET *allocate_htc_bundle_packet(HTC_TARGET *target)
  160. {
  161. HTC_PACKET *pPacket;
  162. HTC_PACKET_QUEUE *pQueueSave;
  163. qdf_nbuf_t netbuf;
  164. LOCK_HTC_TX(target);
  165. if (!target->pBundleFreeList) {
  166. UNLOCK_HTC_TX(target);
  167. netbuf = qdf_nbuf_alloc(NULL,
  168. target->MaxMsgsPerHTCBundle *
  169. target->TargetCreditSize, 0, 4, false);
  170. AR_DEBUG_ASSERT(netbuf);
  171. if (!netbuf)
  172. return NULL;
  173. pPacket = qdf_mem_malloc(sizeof(HTC_PACKET));
  174. AR_DEBUG_ASSERT(pPacket);
  175. if (!pPacket) {
  176. qdf_nbuf_free(netbuf);
  177. return NULL;
  178. }
  179. pQueueSave = qdf_mem_malloc(sizeof(HTC_PACKET_QUEUE));
  180. AR_DEBUG_ASSERT(pQueueSave);
  181. if (!pQueueSave) {
  182. qdf_nbuf_free(netbuf);
  183. qdf_mem_free(pPacket);
  184. return NULL;
  185. }
  186. INIT_HTC_PACKET_QUEUE(pQueueSave);
  187. pPacket->pContext = pQueueSave;
  188. SET_HTC_PACKET_NET_BUF_CONTEXT(pPacket, netbuf);
  189. pPacket->pBuffer = qdf_nbuf_data(netbuf);
  190. pPacket->BufferLength = qdf_nbuf_len(netbuf);
  191. /* store the original head room so that we can restore this
  192. * when we "free" the packet.
  193. * free packet puts the packet back on the free list
  194. */
  195. pPacket->netbufOrigHeadRoom = qdf_nbuf_headroom(netbuf);
  196. return pPacket;
  197. }
  198. /* already done malloc - restore from free list */
  199. pPacket = target->pBundleFreeList;
  200. AR_DEBUG_ASSERT(pPacket);
  201. if (!pPacket) {
  202. UNLOCK_HTC_TX(target);
  203. return NULL;
  204. }
  205. target->pBundleFreeList = (HTC_PACKET *) pPacket->ListLink.pNext;
  206. UNLOCK_HTC_TX(target);
  207. pPacket->ListLink.pNext = NULL;
  208. return pPacket;
  209. }
  210. void free_htc_bundle_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
  211. {
  212. uint32_t curentHeadRoom;
  213. qdf_nbuf_t netbuf;
  214. HTC_PACKET_QUEUE *pQueueSave;
  215. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  216. AR_DEBUG_ASSERT(netbuf);
  217. if (!netbuf) {
  218. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  219. ("\n%s: Invalid netbuf in HTC Packet\n",
  220. __func__));
  221. return;
  222. }
  223. /* HIF adds data to the headroom section of the nbuf, restore thei
  224. * original size. If this is not done, headroom keeps shrinking with
  225. * every HIF send and eventually HIF ends up doing another malloc big
  226. * enough to store the data + its header
  227. */
  228. curentHeadRoom = qdf_nbuf_headroom(netbuf);
  229. qdf_nbuf_pull_head(netbuf,
  230. pPacket->netbufOrigHeadRoom - curentHeadRoom);
  231. qdf_nbuf_trim_tail(netbuf, qdf_nbuf_len(netbuf));
  232. /* restore the pBuffer pointer. HIF changes this */
  233. pPacket->pBuffer = qdf_nbuf_data(netbuf);
  234. pPacket->BufferLength = qdf_nbuf_len(netbuf);
  235. /* restore queue */
  236. pQueueSave = (HTC_PACKET_QUEUE *) pPacket->pContext;
  237. if (qdf_unlikely(!pQueueSave)) {
  238. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  239. ("\n%s: Invalid pQueueSave in HTC Packet\n",
  240. __func__));
  241. AR_DEBUG_ASSERT(pQueueSave);
  242. } else
  243. INIT_HTC_PACKET_QUEUE(pQueueSave);
  244. LOCK_HTC_TX(target);
  245. if (!target->pBundleFreeList) {
  246. target->pBundleFreeList = pPacket;
  247. pPacket->ListLink.pNext = NULL;
  248. } else {
  249. pPacket->ListLink.pNext = (DL_LIST *) target->pBundleFreeList;
  250. target->pBundleFreeList = pPacket;
  251. }
  252. UNLOCK_HTC_TX(target);
  253. }
  254. #if defined(DEBUG_HL_LOGGING) && defined(CONFIG_HL_SUPPORT)
  255. /**
  256. * htc_send_update_tx_bundle_stats() - update tx bundle stats depends
  257. * on max bundle size
  258. * @target: hif context
  259. * @data_len: tx data len
  260. * @TxCreditSize: endpoint tx credit size
  261. *
  262. * Return: None
  263. */
  264. static inline void
  265. htc_send_update_tx_bundle_stats(HTC_TARGET *target,
  266. qdf_size_t data_len,
  267. int TxCreditSize)
  268. {
  269. int index = ((data_len + TxCreditSize - 1) / TxCreditSize) - 1;
  270. if (index < HTC_MAX_MSG_PER_BUNDLE_TX)
  271. target->tx_bundle_stats[index]++;
  272. }
  273. /**
  274. * htc_issue_tx_bundle_stats_inc() - increment in tx bundle stats
  275. * on max bundle size
  276. * @target: hif context
  277. *
  278. * Return: None
  279. */
  280. static inline void
  281. htc_issue_tx_bundle_stats_inc(HTC_TARGET *target)
  282. {
  283. target->tx_bundle_stats[0]++;
  284. }
  285. #else
  286. static inline void
  287. htc_send_update_tx_bundle_stats(HTC_TARGET *target,
  288. qdf_size_t data_len,
  289. int TxCreditSize)
  290. {
  291. }
  292. static inline void
  293. htc_issue_tx_bundle_stats_inc(HTC_TARGET *target)
  294. {
  295. }
  296. #endif
  297. #if defined(HIF_USB) || defined(HIF_SDIO)
  298. #ifdef ENABLE_BUNDLE_TX
  299. static QDF_STATUS htc_send_bundled_netbuf(HTC_TARGET *target,
  300. HTC_ENDPOINT *pEndpoint,
  301. unsigned char *pBundleBuffer,
  302. HTC_PACKET *pPacketTx)
  303. {
  304. qdf_size_t data_len;
  305. QDF_STATUS status;
  306. qdf_nbuf_t bundleBuf;
  307. uint32_t data_attr = 0;
  308. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  309. data_len = pBundleBuffer - qdf_nbuf_data(bundleBuf);
  310. qdf_nbuf_put_tail(bundleBuf, data_len);
  311. SET_HTC_PACKET_INFO_TX(pPacketTx,
  312. target,
  313. pBundleBuffer,
  314. data_len,
  315. pEndpoint->Id, HTC_TX_PACKET_TAG_BUNDLED);
  316. LOCK_HTC_TX(target);
  317. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacketTx);
  318. pEndpoint->ul_outstanding_cnt++;
  319. UNLOCK_HTC_TX(target);
  320. #if DEBUG_BUNDLE
  321. qdf_print(" Send bundle EP%d buffer size:0x%x, total:0x%x, count:%d.",
  322. pEndpoint->Id,
  323. pEndpoint->TxCreditSize,
  324. data_len, data_len / pEndpoint->TxCreditSize);
  325. #endif
  326. htc_send_update_tx_bundle_stats(target, data_len,
  327. pEndpoint->TxCreditSize);
  328. status = hif_send_head(target->hif_dev,
  329. pEndpoint->UL_PipeID,
  330. pEndpoint->Id, data_len,
  331. bundleBuf, data_attr);
  332. if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
  333. HTC_PACKET_QUEUE requeue;
  334. qdf_print("hif_send_head failed(len=%zu).", data_len);
  335. INIT_HTC_PACKET_QUEUE(&requeue);
  336. LOCK_HTC_TX(target);
  337. pEndpoint->ul_outstanding_cnt--;
  338. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacketTx);
  339. if (pPacketTx->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
  340. HTC_PACKET *temp_packet;
  341. HTC_PACKET_QUEUE *packet_queue =
  342. (HTC_PACKET_QUEUE *)pPacketTx->pContext;
  343. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(packet_queue,
  344. temp_packet) {
  345. HTC_PACKET_ENQUEUE(&requeue, temp_packet);
  346. } HTC_PACKET_QUEUE_ITERATE_END;
  347. UNLOCK_HTC_TX(target);
  348. free_htc_bundle_packet(target, pPacketTx);
  349. LOCK_HTC_TX(target);
  350. } else {
  351. HTC_PACKET_ENQUEUE(&requeue, pPacketTx);
  352. }
  353. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  354. &requeue);
  355. UNLOCK_HTC_TX(target);
  356. }
  357. return status;
  358. }
  359. #ifdef QCA_TX_PADDING_CREDIT_SUPPORT
  360. #define SDIO_BLOCK_SIZE 512
  361. static int htc_tx_pad_credit_avail(HTC_ENDPOINT *ep)
  362. {
  363. int ret = 0;
  364. if (!ep || !ep->EpCallBacks.pContext ||
  365. !ep->EpCallBacks.ep_padding_credit_update)
  366. return 1;
  367. ret = ep->EpCallBacks.ep_padding_credit_update(ep->EpCallBacks.pContext,
  368. 0);
  369. if (ret < 2)
  370. AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s ret %d\n", __func__, ret));
  371. return ret;
  372. }
  373. static bool htc_handle_extra_tx_credit(HTC_ENDPOINT *ep,
  374. HTC_PACKET *p_last_htc_pkt,
  375. unsigned char *p_last_pkt_bundle_buffer,
  376. unsigned char **p_bundle_buffer,
  377. int tot_data_len)
  378. {
  379. bool extra_tx_credit = FALSE;
  380. HTC_FRAME_HDR *p_htc_hdr;
  381. int first_buf_bundled_len = 0, last_buf_len = 0;
  382. int sdio_pad = 0, free_space = 0;
  383. int (*update_ep_padding_credit)(void *, int);
  384. update_ep_padding_credit = ep->EpCallBacks.ep_padding_credit_update;
  385. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  386. ("%s Tot data_len = %d\n", __func__, tot_data_len));
  387. if (!p_last_htc_pkt)
  388. return extra_tx_credit;
  389. last_buf_len = (p_last_htc_pkt->ActualLength + HTC_HDR_LENGTH);
  390. if (tot_data_len != last_buf_len) {
  391. first_buf_bundled_len = tot_data_len - ep->TxCreditSize;
  392. free_space = tot_data_len -
  393. (first_buf_bundled_len + last_buf_len);
  394. } else {
  395. free_space = ep->TxCreditSize - tot_data_len;
  396. }
  397. sdio_pad = SDIO_BLOCK_SIZE - ((first_buf_bundled_len + last_buf_len) %
  398. SDIO_BLOCK_SIZE);
  399. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  400. ("%s first_buf_bundled_len = %d last_buf_len = %d\n",
  401. __func__, first_buf_bundled_len, last_buf_len));
  402. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  403. ("%s sdio_pad = %d free_space = %d\n", __func__,
  404. sdio_pad, free_space));
  405. if (sdio_pad <= free_space) {
  406. if (p_bundle_buffer && *p_bundle_buffer) {
  407. /* Align Tx bundled buf to avoid a extra Padding buf */
  408. *p_bundle_buffer -= (free_space - sdio_pad);
  409. }
  410. } else {
  411. /* Extra Padding Buffer needed, consume extra tx credit */
  412. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  413. ("%s Used a Tx credit for Padding Buffer\n",
  414. __func__));
  415. p_htc_hdr = (HTC_FRAME_HDR *)(p_last_pkt_bundle_buffer);
  416. p_htc_hdr->Flags |= HTC_FLAGS_PADDING_CHECK;
  417. extra_tx_credit = TRUE;
  418. if (ep->EpCallBacks.ep_padding_credit_update) {
  419. /* Decrement 1 credit at host,
  420. * due to extra tx credit consumed by padding buffer
  421. */
  422. update_ep_padding_credit(ep->EpCallBacks.pContext, -1);
  423. }
  424. }
  425. return extra_tx_credit;
  426. }
  427. #else
  428. static int htc_tx_pad_credit_avail(HTC_ENDPOINT *ep)
  429. {
  430. return 1;
  431. }
  432. static bool htc_handle_extra_tx_credit(HTC_ENDPOINT *ep,
  433. HTC_PACKET *p_last_htc_pkt,
  434. unsigned char *p_last_pkt_bundle_buffer,
  435. unsigned char **p_bundle_buffer,
  436. int tot_data_len)
  437. {
  438. return FALSE;
  439. }
  440. #endif
  441. /**
  442. * htc_issue_packets_bundle() - HTC function to send bundle packets from a queue
  443. * @target: HTC target on which packets need to be sent
  444. * @pEndpoint: logical endpoint on which packets needs to be sent
  445. * @pPktQueue: HTC packet queue containing the list of packets to be sent
  446. *
  447. * Return: void
  448. */
  449. static void htc_issue_packets_bundle(HTC_TARGET *target,
  450. HTC_ENDPOINT *pEndpoint,
  451. HTC_PACKET_QUEUE *pPktQueue)
  452. {
  453. int i, frag_count, nbytes;
  454. qdf_nbuf_t netbuf, bundleBuf;
  455. unsigned char *pBundleBuffer = NULL;
  456. HTC_PACKET *pPacket = NULL, *pPacketTx = NULL;
  457. HTC_FRAME_HDR *pHtcHdr;
  458. int last_credit_pad = 0;
  459. int creditPad, creditRemainder, transferLength, bundlesSpaceRemaining =
  460. 0;
  461. HTC_PACKET_QUEUE *pQueueSave = NULL;
  462. HTC_PACKET *p_last_htc_pkt = NULL;
  463. unsigned char *p_last_pkt_bundle_buffer = NULL;
  464. bundlesSpaceRemaining =
  465. target->MaxMsgsPerHTCBundle * pEndpoint->TxCreditSize;
  466. pPacketTx = allocate_htc_bundle_packet(target);
  467. if (!pPacketTx) {
  468. /* good time to panic */
  469. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  470. ("allocate_htc_bundle_packet failed\n"));
  471. AR_DEBUG_ASSERT(false);
  472. return;
  473. }
  474. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  475. pBundleBuffer = qdf_nbuf_data(bundleBuf);
  476. pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
  477. while (1) {
  478. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  479. if (htc_tx_pad_credit_avail(pEndpoint) < 1)
  480. break;
  481. }
  482. pPacket = htc_packet_dequeue(pPktQueue);
  483. if (!pPacket)
  484. break;
  485. creditPad = 0;
  486. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  487. creditRemainder = transferLength % pEndpoint->TxCreditSize;
  488. if (creditRemainder != 0) {
  489. if (transferLength < pEndpoint->TxCreditSize) {
  490. creditPad = pEndpoint->TxCreditSize -
  491. transferLength;
  492. } else {
  493. creditPad = creditRemainder;
  494. }
  495. transferLength += creditPad;
  496. }
  497. if (bundlesSpaceRemaining < transferLength) {
  498. htc_handle_extra_tx_credit(pEndpoint, p_last_htc_pkt,
  499. p_last_pkt_bundle_buffer,
  500. &pBundleBuffer,
  501. pBundleBuffer -
  502. qdf_nbuf_data(bundleBuf));
  503. /* send out previous buffer */
  504. htc_send_bundled_netbuf(target, pEndpoint,
  505. pBundleBuffer - last_credit_pad,
  506. pPacketTx);
  507. /* One packet has been dequeued from sending queue when enter
  508. * this loop, so need to add 1 back for this checking.
  509. */
  510. if ((HTC_PACKET_QUEUE_DEPTH(pPktQueue) + 1) <
  511. HTC_MIN_MSG_PER_BUNDLE) {
  512. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  513. return;
  514. }
  515. bundlesSpaceRemaining =
  516. target->MaxMsgsPerHTCBundle *
  517. pEndpoint->TxCreditSize;
  518. pPacketTx = allocate_htc_bundle_packet(target);
  519. if (!pPacketTx) {
  520. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  521. /* good time to panic */
  522. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  523. ("allocate_htc_bundle_packet failed\n"));
  524. AR_DEBUG_ASSERT(false);
  525. return;
  526. }
  527. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  528. pBundleBuffer = qdf_nbuf_data(bundleBuf);
  529. pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
  530. }
  531. p_last_htc_pkt = pPacket;
  532. p_last_pkt_bundle_buffer = pBundleBuffer;
  533. bundlesSpaceRemaining -= transferLength;
  534. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  535. if (hif_get_bus_type(target->hif_dev) != QDF_BUS_TYPE_USB) {
  536. pHtcHdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(
  537. netbuf, 0);
  538. HTC_WRITE32(pHtcHdr,
  539. SM(pPacket->ActualLength,
  540. HTC_FRAME_HDR_PAYLOADLEN) |
  541. SM(pPacket->PktInfo.AsTx.SendFlags |
  542. HTC_FLAGS_SEND_BUNDLE,
  543. HTC_FRAME_HDR_FLAGS) |
  544. SM(pPacket->Endpoint,
  545. HTC_FRAME_HDR_ENDPOINTID));
  546. HTC_WRITE32((uint32_t *) pHtcHdr + 1,
  547. SM(pPacket->PktInfo.AsTx.SeqNo,
  548. HTC_FRAME_HDR_CONTROLBYTES1) | SM(creditPad,
  549. HTC_FRAME_HDR_RESERVED));
  550. pHtcHdr->reserved = creditPad;
  551. }
  552. frag_count = qdf_nbuf_get_num_frags(netbuf);
  553. nbytes = pPacket->ActualLength + HTC_HDR_LENGTH;
  554. for (i = 0; i < frag_count && nbytes > 0; i++) {
  555. int frag_len = qdf_nbuf_get_frag_len(netbuf, i);
  556. unsigned char *frag_addr =
  557. qdf_nbuf_get_frag_vaddr(netbuf, i);
  558. if (frag_len > nbytes)
  559. frag_len = nbytes;
  560. qdf_mem_copy(pBundleBuffer, frag_addr, frag_len);
  561. nbytes -= frag_len;
  562. pBundleBuffer += frag_len;
  563. }
  564. HTC_PACKET_ENQUEUE(pQueueSave, pPacket);
  565. pBundleBuffer += creditPad;
  566. /* last one can't be packed. */
  567. if (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)
  568. last_credit_pad = creditPad;
  569. }
  570. /* send out remaining buffer */
  571. if (pBundleBuffer != qdf_nbuf_data(bundleBuf)) {
  572. htc_handle_extra_tx_credit(pEndpoint, p_last_htc_pkt,
  573. p_last_pkt_bundle_buffer,
  574. &pBundleBuffer,
  575. pBundleBuffer -
  576. qdf_nbuf_data(bundleBuf));
  577. htc_send_bundled_netbuf(target, pEndpoint,
  578. pBundleBuffer - last_credit_pad,
  579. pPacketTx);
  580. } else {
  581. free_htc_bundle_packet(target, pPacketTx);
  582. }
  583. }
  584. #endif /* ENABLE_BUNDLE_TX */
  585. #else
  586. static int htc_tx_pad_credit_avail(HTC_ENDPOINT *ep)
  587. {
  588. return 1;
  589. }
  590. bool htc_handle_extra_tx_credit(HTC_ENDPOINT *ep,
  591. HTC_PACKET *p_last_htc_pkt,
  592. unsigned char *p_last_pkt_bundle_buffer,
  593. unsigned char **p_bundle_buffer,
  594. int tot_data_len);
  595. bool htc_handle_extra_tx_credit(HTC_ENDPOINT *ep,
  596. HTC_PACKET *p_last_htc_pkt,
  597. unsigned char *p_last_pkt_bundle_buffer,
  598. unsigned char **p_bundle_buffer,
  599. int tot_data_len)
  600. {
  601. return FALSE;
  602. }
  603. static void htc_issue_packets_bundle(HTC_TARGET *target,
  604. HTC_ENDPOINT *pEndpoint,
  605. HTC_PACKET_QUEUE *pPktQueue)
  606. {
  607. }
  608. #endif
  609. /**
  610. * htc_issue_packets() - HTC function to send packets from a queue
  611. * @target: HTC target on which packets need to be sent
  612. * @pEndpoint: logical endpoint on which packets needs to be sent
  613. * @pPktQueue: HTC packet queue containing the list of packets to be sent
  614. *
  615. * Return: QDF_STATUS_SUCCESS on success and error QDF status on failure
  616. */
  617. static QDF_STATUS htc_issue_packets(HTC_TARGET *target,
  618. HTC_ENDPOINT *pEndpoint,
  619. HTC_PACKET_QUEUE *pPktQueue)
  620. {
  621. QDF_STATUS status = QDF_STATUS_SUCCESS;
  622. qdf_nbuf_t netbuf;
  623. HTC_PACKET *pPacket = NULL;
  624. uint16_t payloadLen;
  625. HTC_FRAME_HDR *pHtcHdr;
  626. uint32_t data_attr = 0;
  627. enum qdf_bus_type bus_type;
  628. QDF_STATUS ret;
  629. bool rt_put = false;
  630. bool used_extra_tx_credit = false;
  631. uint8_t *buf = NULL;
  632. int (*update_ep_padding_credit)(void *, int);
  633. void *ctx = NULL;
  634. update_ep_padding_credit =
  635. pEndpoint->EpCallBacks.ep_padding_credit_update;
  636. bus_type = hif_get_bus_type(target->hif_dev);
  637. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  638. ("+htc_issue_packets: Queue: %pK, Pkts %d\n", pPktQueue,
  639. HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
  640. while (true) {
  641. if (HTC_TX_BUNDLE_ENABLED(target) &&
  642. HTC_PACKET_QUEUE_DEPTH(pPktQueue) >=
  643. HTC_MIN_MSG_PER_BUNDLE) {
  644. switch (bus_type) {
  645. case QDF_BUS_TYPE_SDIO:
  646. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
  647. break;
  648. if (update_ep_padding_credit) {
  649. if (htc_tx_pad_credit_avail
  650. (pEndpoint) < 1)
  651. break;
  652. }
  653. case QDF_BUS_TYPE_USB:
  654. htc_issue_packets_bundle(target,
  655. pEndpoint,
  656. pPktQueue);
  657. break;
  658. default:
  659. break;
  660. }
  661. }
  662. /* if not bundling or there was a packet that could not be
  663. * placed in a bundle, and send it by normal way
  664. */
  665. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  666. if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
  667. status = QDF_STATUS_E_FAILURE;
  668. break;
  669. }
  670. }
  671. pPacket = htc_packet_dequeue(pPktQueue);
  672. if (!pPacket) {
  673. /* local queue is fully drained */
  674. break;
  675. }
  676. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  677. AR_DEBUG_ASSERT(netbuf);
  678. /* Non-credit enabled endpoints have been mapped and setup by
  679. * now, so no need to revisit the HTC headers
  680. */
  681. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  682. payloadLen = pPacket->ActualLength;
  683. /* setup HTC frame header */
  684. pHtcHdr = (HTC_FRAME_HDR *)
  685. qdf_nbuf_get_frag_vaddr(netbuf, 0);
  686. if (qdf_unlikely(!pHtcHdr)) {
  687. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  688. ("%s Invalid pHtcHdr\n",
  689. __func__));
  690. AR_DEBUG_ASSERT(pHtcHdr);
  691. status = QDF_STATUS_E_FAILURE;
  692. break;
  693. }
  694. HTC_WRITE32(pHtcHdr,
  695. SM(payloadLen,
  696. HTC_FRAME_HDR_PAYLOADLEN) |
  697. SM(pPacket->PktInfo.AsTx.SendFlags,
  698. HTC_FRAME_HDR_FLAGS) |
  699. SM(pPacket->Endpoint,
  700. HTC_FRAME_HDR_ENDPOINTID));
  701. HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
  702. SM(pPacket->PktInfo.AsTx.SeqNo,
  703. HTC_FRAME_HDR_CONTROLBYTES1));
  704. /*
  705. * Now that the HTC frame header has been added, the
  706. * netbuf can be mapped. This only applies to non-data
  707. * frames, since data frames were already mapped as they
  708. * entered into the driver.
  709. */
  710. ret = qdf_nbuf_map(target->osdev,
  711. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  712. QDF_DMA_TO_DEVICE);
  713. if (ret != QDF_STATUS_SUCCESS) {
  714. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  715. ("%s nbuf Map Fail Endpnt %pK\n",
  716. __func__, pEndpoint));
  717. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  718. status = QDF_STATUS_E_FAILURE;
  719. break;
  720. }
  721. pPacket->PktInfo.AsTx.Flags |=
  722. HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  723. }
  724. if (!pEndpoint->async_update) {
  725. LOCK_HTC_TX(target);
  726. }
  727. /* store in look up queue to match completions */
  728. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  729. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  730. pEndpoint->ul_outstanding_cnt++;
  731. if (!pEndpoint->async_update) {
  732. UNLOCK_HTC_TX(target);
  733. hif_send_complete_check(target->hif_dev,
  734. pEndpoint->UL_PipeID, false);
  735. }
  736. htc_packet_set_magic_cookie(pPacket, HTC_PACKET_MAGIC_COOKIE);
  737. /*
  738. * For HTT messages without a response from fw,
  739. * do the runtime put here.
  740. * otherwise runtime put will be done when the fw response comes
  741. */
  742. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_RUNTIME_PUT)
  743. rt_put = true;
  744. #if DEBUG_BUNDLE
  745. qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
  746. pEndpoint->Id,
  747. pEndpoint->TxCreditSize,
  748. HTC_HDR_LENGTH + pPacket->ActualLength);
  749. #endif
  750. buf = (uint8_t *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  751. used_extra_tx_credit =
  752. htc_handle_extra_tx_credit(pEndpoint, pPacket, buf,
  753. NULL, pPacket->ActualLength +
  754. HTC_HDR_LENGTH);
  755. status = hif_send_head(target->hif_dev,
  756. pEndpoint->UL_PipeID, pEndpoint->Id,
  757. HTC_HDR_LENGTH + pPacket->ActualLength,
  758. netbuf, data_attr);
  759. if (status != QDF_STATUS_SUCCESS) {
  760. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  761. if (used_extra_tx_credit) {
  762. ctx = pEndpoint->EpCallBacks.pContext;
  763. update_ep_padding_credit(ctx, 1);
  764. }
  765. }
  766. }
  767. htc_issue_tx_bundle_stats_inc(target);
  768. target->ce_send_cnt++;
  769. if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
  770. if (status != QDF_STATUS_E_RESOURCES) {
  771. /* TODO : if more than 1 endpoint maps to the
  772. * same PipeID it is possible to run out of
  773. * resources in the HIF layer. Don't emit the
  774. * error
  775. */
  776. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  777. ("hif_send Failed status:%d\n",
  778. status));
  779. }
  780. /* only unmap if we mapped in this function */
  781. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  782. qdf_nbuf_unmap(target->osdev,
  783. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  784. QDF_DMA_TO_DEVICE);
  785. pPacket->PktInfo.AsTx.Flags &=
  786. ~HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  787. }
  788. if (!pEndpoint->async_update) {
  789. LOCK_HTC_TX(target);
  790. }
  791. target->ce_send_cnt--;
  792. pEndpoint->ul_outstanding_cnt--;
  793. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  794. htc_packet_set_magic_cookie(pPacket, 0);
  795. /* put it back into the callers queue */
  796. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  797. /* reclaim credits */
  798. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue,
  799. pPacket) {
  800. pEndpoint->TxCredits +=
  801. pPacket->PktInfo.AsTx.CreditsUsed;
  802. } HTC_PACKET_QUEUE_ITERATE_END;
  803. if (!pEndpoint->async_update) {
  804. UNLOCK_HTC_TX(target);
  805. }
  806. break;
  807. }
  808. if (rt_put) {
  809. hif_pm_runtime_put(target->hif_dev);
  810. rt_put = false;
  811. }
  812. }
  813. if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
  814. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  815. ("htc_issue_packets, failed pkt:0x%pK status:%d",
  816. pPacket, status));
  817. }
  818. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_issue_packets\n"));
  819. return status;
  820. }
  821. #ifdef FEATURE_RUNTIME_PM
  822. /**
  823. * extract_htc_pm_packets(): move pm packets from endpoint into queue
  824. * @endpoint: which enpoint to extract packets from
  825. * @queue: a queue to store extracted packets in.
  826. *
  827. * remove pm packets from the endpoint's tx queue.
  828. * queue them into a queue
  829. */
  830. static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
  831. HTC_PACKET_QUEUE *queue)
  832. {
  833. HTC_PACKET *packet;
  834. /* only WMI endpoint has power management packets */
  835. if (endpoint->service_id != WMI_CONTROL_SVC)
  836. return;
  837. ITERATE_OVER_LIST_ALLOW_REMOVE(&endpoint->TxQueue.QueueHead, packet,
  838. HTC_PACKET, ListLink) {
  839. if (packet->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_AUTO_PM) {
  840. HTC_PACKET_REMOVE(&endpoint->TxQueue, packet);
  841. HTC_PACKET_ENQUEUE(queue, packet);
  842. }
  843. } ITERATE_END
  844. }
  845. /**
  846. * queue_htc_pm_packets(): queue pm packets with priority
  847. * @endpoint: enpoint to queue packets to
  848. * @queue: queue of pm packets to enque
  849. *
  850. * suspend resume packets get special treatment & priority.
  851. * need to queue them at the front of the queue.
  852. */
  853. static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
  854. HTC_PACKET_QUEUE *queue)
  855. {
  856. if (endpoint->service_id != WMI_CONTROL_SVC)
  857. return;
  858. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&endpoint->TxQueue, queue);
  859. }
  860. #else
  861. static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
  862. HTC_PACKET_QUEUE *queue)
  863. {}
  864. static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
  865. HTC_PACKET_QUEUE *queue)
  866. {}
  867. #endif
  868. /**
  869. * get_htc_send_packets_credit_based() - get packets based on available credits
  870. * @target: HTC target on which packets need to be sent
  871. * @pEndpoint: logical endpoint on which packets needs to be sent
  872. * @pQueue: HTC packet queue containing the list of packets to be sent
  873. *
  874. * Get HTC send packets from TX queue on an endpoint based on available credits.
  875. * The function moves the packets from TX queue of the endpoint to pQueue.
  876. *
  877. * Return: None
  878. */
  879. static void get_htc_send_packets_credit_based(HTC_TARGET *target,
  880. HTC_ENDPOINT *pEndpoint,
  881. HTC_PACKET_QUEUE *pQueue)
  882. {
  883. int creditsRequired;
  884. int remainder;
  885. uint8_t sendFlags;
  886. HTC_PACKET *pPacket;
  887. unsigned int transferLength;
  888. HTC_PACKET_QUEUE *tx_queue;
  889. HTC_PACKET_QUEUE pm_queue;
  890. bool do_pm_get = false;
  891. int ret;
  892. /*** NOTE : the TX lock is held when this function is called ***/
  893. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  894. ("+get_htc_send_packets_credit_based\n"));
  895. INIT_HTC_PACKET_QUEUE(&pm_queue);
  896. extract_htc_pm_packets(pEndpoint, &pm_queue);
  897. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  898. tx_queue = &pEndpoint->TxQueue;
  899. do_pm_get = true;
  900. } else {
  901. tx_queue = &pm_queue;
  902. }
  903. /* loop until we can grab as many packets out of the queue as we can */
  904. while (true) {
  905. if (do_pm_get) {
  906. ret = hif_pm_runtime_get(target->hif_dev);
  907. if (ret) {
  908. /* bus suspended, runtime resume issued */
  909. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  910. if (ret == -EAGAIN) {
  911. pPacket = htc_get_pkt_at_head(tx_queue);
  912. if (!pPacket)
  913. break;
  914. log_packet_info(target, pPacket);
  915. }
  916. break;
  917. }
  918. }
  919. sendFlags = 0;
  920. /* get packet at head, but don't remove it */
  921. pPacket = htc_get_pkt_at_head(tx_queue);
  922. if (!pPacket) {
  923. if (do_pm_get)
  924. hif_pm_runtime_put(target->hif_dev);
  925. break;
  926. }
  927. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  928. (" Got head packet:%pK , Queue Depth: %d\n",
  929. pPacket,
  930. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  931. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  932. if (transferLength <= pEndpoint->TxCreditSize) {
  933. creditsRequired = 1;
  934. } else {
  935. /* figure out how many credits this message requires */
  936. creditsRequired =
  937. transferLength / pEndpoint->TxCreditSize;
  938. remainder = transferLength % pEndpoint->TxCreditSize;
  939. if (remainder)
  940. creditsRequired++;
  941. }
  942. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  943. (" Credits Required:%d Got:%d\n",
  944. creditsRequired, pEndpoint->TxCredits));
  945. if (pEndpoint->Id == ENDPOINT_0) {
  946. /*
  947. * endpoint 0 is special, it always has a credit and
  948. * does not require credit based flow control
  949. */
  950. creditsRequired = 0;
  951. } else {
  952. if (pEndpoint->TxCredits < creditsRequired) {
  953. #if DEBUG_CREDIT
  954. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  955. ("EP%d,No Credit now.%d < %d\n",
  956. pEndpoint->Id,
  957. pEndpoint->TxCredits,
  958. creditsRequired));
  959. #endif
  960. if (do_pm_get)
  961. hif_pm_runtime_put(target->hif_dev);
  962. break;
  963. }
  964. pEndpoint->TxCredits -= creditsRequired;
  965. INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
  966. creditsRequired);
  967. /* check if we need credits back from the target */
  968. if (pEndpoint->TxCredits <=
  969. pEndpoint->TxCreditsPerMaxMsg) {
  970. /* tell the target we need credits ASAP! */
  971. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  972. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  973. htc_credit_record(HTC_REQUEST_CREDIT,
  974. pEndpoint->TxCredits,
  975. HTC_PACKET_QUEUE_DEPTH
  976. (tx_queue));
  977. }
  978. INC_HTC_EP_STAT(pEndpoint,
  979. TxCreditLowIndications, 1);
  980. #if DEBUG_CREDIT
  981. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  982. (" EP%d Needs Credits\n",
  983. pEndpoint->Id));
  984. #endif
  985. }
  986. }
  987. /* now we can fully dequeue */
  988. pPacket = htc_packet_dequeue(tx_queue);
  989. if (pPacket) {
  990. /* save the number of credits this packet consumed */
  991. pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
  992. /* save send flags */
  993. pPacket->PktInfo.AsTx.SendFlags = sendFlags;
  994. /* queue this packet into the caller's queue */
  995. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  996. }
  997. }
  998. if (!HTC_QUEUE_EMPTY(&pm_queue))
  999. queue_htc_pm_packets(pEndpoint, &pm_queue);
  1000. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1001. ("-get_htc_send_packets_credit_based\n"));
  1002. }
  1003. static void get_htc_send_packets(HTC_TARGET *target,
  1004. HTC_ENDPOINT *pEndpoint,
  1005. HTC_PACKET_QUEUE *pQueue, int Resources)
  1006. {
  1007. HTC_PACKET *pPacket;
  1008. HTC_PACKET_QUEUE *tx_queue;
  1009. HTC_PACKET_QUEUE pm_queue;
  1010. bool do_pm_get = false;
  1011. int ret;
  1012. /*** NOTE : the TX lock is held when this function is called ***/
  1013. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1014. ("+get_htc_send_packets %d resources\n", Resources));
  1015. INIT_HTC_PACKET_QUEUE(&pm_queue);
  1016. extract_htc_pm_packets(pEndpoint, &pm_queue);
  1017. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  1018. tx_queue = &pEndpoint->TxQueue;
  1019. do_pm_get = true;
  1020. } else {
  1021. tx_queue = &pm_queue;
  1022. }
  1023. /* loop until we can grab as many packets out of the queue as we can */
  1024. while (Resources > 0) {
  1025. int num_frags;
  1026. if (do_pm_get) {
  1027. ret = hif_pm_runtime_get(target->hif_dev);
  1028. if (ret) {
  1029. /* bus suspended, runtime resume issued */
  1030. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  1031. if (ret == -EAGAIN) {
  1032. pPacket = htc_get_pkt_at_head(tx_queue);
  1033. if (!pPacket)
  1034. break;
  1035. log_packet_info(target, pPacket);
  1036. }
  1037. break;
  1038. }
  1039. }
  1040. pPacket = htc_packet_dequeue(tx_queue);
  1041. if (!pPacket) {
  1042. if (do_pm_get)
  1043. hif_pm_runtime_put(target->hif_dev);
  1044. break;
  1045. }
  1046. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1047. (" Got packet:%pK , New Queue Depth: %d\n",
  1048. pPacket,
  1049. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  1050. /* For non-credit path the sequence number is already embedded
  1051. * in the constructed HTC header
  1052. */
  1053. pPacket->PktInfo.AsTx.SendFlags = 0;
  1054. pPacket->PktInfo.AsTx.CreditsUsed = 0;
  1055. /* queue this packet into the caller's queue */
  1056. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  1057. /*
  1058. * FIX THIS:
  1059. * For now, avoid calling qdf_nbuf_get_num_frags before calling
  1060. * qdf_nbuf_map, because the MacOS version of qdf_nbuf_t doesn't
  1061. * support qdf_nbuf_get_num_frags until after qdf_nbuf_map has
  1062. * been done.
  1063. * Assume that the non-data netbufs, i.e. WMI message netbufs,
  1064. * consist of a single fragment.
  1065. */
  1066. /* WMI messages are in a single-fragment network buf */
  1067. num_frags =
  1068. (pPacket->PktInfo.AsTx.
  1069. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1 :
  1070. qdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
  1071. (pPacket));
  1072. Resources -= num_frags;
  1073. }
  1074. if (!HTC_QUEUE_EMPTY(&pm_queue))
  1075. queue_htc_pm_packets(pEndpoint, &pm_queue);
  1076. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets\n"));
  1077. }
  1078. /**
  1079. * htc_try_send() - Send packets in a queue on an endpoint
  1080. * @target: HTC target on which packets need to be sent
  1081. * @pEndpoint: logical endpoint on which packets needs to be sent
  1082. * @pCallersSendQueue: packet queue containing the list of packets to be sent
  1083. *
  1084. * Return: enum HTC_SEND_QUEUE_RESULT indicates whether the packet was queued to
  1085. * be sent or the packet should be dropped by the upper layer
  1086. */
  1087. static enum HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
  1088. HTC_ENDPOINT *pEndpoint,
  1089. HTC_PACKET_QUEUE *pCallersSendQueue)
  1090. {
  1091. /* temp queue to hold packets at various stages */
  1092. HTC_PACKET_QUEUE sendQueue;
  1093. HTC_PACKET *pPacket;
  1094. int tx_resources;
  1095. int overflow;
  1096. enum HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
  1097. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%pK Depth:%d)\n",
  1098. pCallersSendQueue,
  1099. (pCallersSendQueue ==
  1100. NULL) ? 0 :
  1101. HTC_PACKET_QUEUE_DEPTH
  1102. (pCallersSendQueue)));
  1103. /* init the local send queue */
  1104. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1105. do {
  1106. /* caller didn't provide a queue, just wants us to check
  1107. * queues and send
  1108. */
  1109. if (!pCallersSendQueue)
  1110. break;
  1111. if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
  1112. /* empty queue */
  1113. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  1114. HTC_PKT_Q_EMPTY);
  1115. result = HTC_SEND_QUEUE_DROP;
  1116. break;
  1117. }
  1118. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
  1119. pEndpoint->MaxTxQueueDepth) {
  1120. /* we've already overflowed */
  1121. overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  1122. } else {
  1123. /* figure out how much we will overflow by */
  1124. overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  1125. overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  1126. /* get how much we will overflow the TX queue by */
  1127. overflow -= pEndpoint->MaxTxQueueDepth;
  1128. }
  1129. /* if overflow is negative or zero, we are okay */
  1130. if (overflow > 0) {
  1131. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1132. ("Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d\n",
  1133. pEndpoint->Id, overflow,
  1134. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  1135. TxQueue),
  1136. pEndpoint->MaxTxQueueDepth));
  1137. }
  1138. if ((overflow <= 0)
  1139. || (!pEndpoint->EpCallBacks.EpSendFull)) {
  1140. /* all packets will fit or caller did not provide send
  1141. * full indication handler
  1142. * just move all of them to local sendQueue object
  1143. */
  1144. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
  1145. pCallersSendQueue);
  1146. } else {
  1147. int i;
  1148. int goodPkts =
  1149. HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
  1150. overflow;
  1151. A_ASSERT(goodPkts >= 0);
  1152. /* we have overflowed and callback is provided. Dequeue
  1153. * all non-overflow packets into the sendqueue
  1154. */
  1155. for (i = 0; i < goodPkts; i++) {
  1156. /* pop off caller's queue */
  1157. pPacket = htc_packet_dequeue(pCallersSendQueue);
  1158. A_ASSERT(pPacket);
  1159. if (pPacket)
  1160. /* insert into local queue */
  1161. HTC_PACKET_ENQUEUE(&sendQueue,
  1162. pPacket);
  1163. }
  1164. /* the caller's queue has all the packets that won't fit
  1165. * walk through the caller's queue and indicate each one
  1166. * to the send full handler
  1167. */
  1168. ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
  1169. QueueHead, pPacket,
  1170. HTC_PACKET, ListLink) {
  1171. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1172. ("Indicating overflowed TX packet: %pK\n",
  1173. pPacket));
  1174. /*
  1175. * Remove headroom reserved for HTC_FRAME_HDR
  1176. * before giving the packet back to the user via
  1177. * the EpSendFull callback.
  1178. */
  1179. restore_tx_packet(target, pPacket);
  1180. if (pEndpoint->EpCallBacks.
  1181. EpSendFull(pEndpoint->EpCallBacks.pContext,
  1182. pPacket) == HTC_SEND_FULL_DROP) {
  1183. /* callback wants the packet dropped */
  1184. INC_HTC_EP_STAT(pEndpoint, TxDropped,
  1185. 1);
  1186. /* leave this one in the caller's queue
  1187. * for cleanup
  1188. */
  1189. } else {
  1190. /* callback wants to keep this packet,
  1191. * remove from caller's queue
  1192. */
  1193. HTC_PACKET_REMOVE(pCallersSendQueue,
  1194. pPacket);
  1195. /* put it in the send queue
  1196. * add HTC_FRAME_HDR space reservation
  1197. * again
  1198. */
  1199. qdf_nbuf_push_head
  1200. (GET_HTC_PACKET_NET_BUF_CONTEXT
  1201. (pPacket),
  1202. sizeof(HTC_FRAME_HDR));
  1203. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  1204. }
  1205. }
  1206. ITERATE_END;
  1207. if (HTC_QUEUE_EMPTY(&sendQueue)) {
  1208. /* no packets made it in, caller will cleanup */
  1209. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  1210. HTC_SEND_Q_EMPTY);
  1211. result = HTC_SEND_QUEUE_DROP;
  1212. break;
  1213. }
  1214. }
  1215. } while (false);
  1216. if (result != HTC_SEND_QUEUE_OK) {
  1217. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: %d\n",
  1218. result));
  1219. return result;
  1220. }
  1221. LOCK_HTC_TX(target);
  1222. if (!HTC_QUEUE_EMPTY(&sendQueue)) {
  1223. if (target->is_nodrop_pkt) {
  1224. /*
  1225. * nodrop pkts have higher priority than normal pkts,
  1226. * insert nodrop pkt to head for proper
  1227. * start/termination of test.
  1228. */
  1229. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1230. &sendQueue);
  1231. target->is_nodrop_pkt = false;
  1232. } else {
  1233. /* transfer packets to tail */
  1234. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
  1235. &sendQueue);
  1236. A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
  1237. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1238. }
  1239. }
  1240. /* increment tx processing count on entry */
  1241. if (qdf_atomic_inc_return(&pEndpoint->TxProcessCount) > 1) {
  1242. /* another thread or task is draining the TX queues on this
  1243. * endpoint that thread will reset the tx processing count when
  1244. * the queue is drained
  1245. */
  1246. qdf_atomic_dec(&pEndpoint->TxProcessCount);
  1247. UNLOCK_HTC_TX(target);
  1248. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy)\n"));
  1249. return HTC_SEND_QUEUE_OK;
  1250. }
  1251. /***** beyond this point only 1 thread may enter ******/
  1252. /* now drain the endpoint TX queue for transmission as long as we have
  1253. * enough transmit resources
  1254. */
  1255. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1256. tx_resources =
  1257. hif_get_free_queue_number(target->hif_dev,
  1258. pEndpoint->UL_PipeID);
  1259. } else {
  1260. tx_resources = 0;
  1261. }
  1262. while (true) {
  1263. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0)
  1264. break;
  1265. if (pEndpoint->async_update &&
  1266. (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) &&
  1267. (!tx_resources))
  1268. break;
  1269. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1270. #if DEBUG_CREDIT
  1271. int cred = pEndpoint->TxCredits;
  1272. #endif
  1273. /* credit based mechanism provides flow control based on
  1274. * target transmit resource availability, we assume that
  1275. * the HIF layer will always have bus resources greater
  1276. * than target transmit resources
  1277. */
  1278. get_htc_send_packets_credit_based(target, pEndpoint,
  1279. &sendQueue);
  1280. #if DEBUG_CREDIT
  1281. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1282. if (cred - pEndpoint->TxCredits > 0) {
  1283. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1284. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  1285. pEndpoint->Id, cred,
  1286. cred -
  1287. pEndpoint->TxCredits,
  1288. pEndpoint->TxCredits));
  1289. }
  1290. }
  1291. #endif
  1292. } else {
  1293. /*
  1294. * Header and payload belongs to the different fragments and
  1295. * consume 2 resource for one HTC package but USB combine into
  1296. * one transfer.And one WMI message only consumes one single
  1297. * resource.
  1298. */
  1299. if (HTC_TX_BUNDLE_ENABLED(target) && tx_resources &&
  1300. hif_get_bus_type(target->hif_dev) ==
  1301. QDF_BUS_TYPE_USB) {
  1302. if (pEndpoint->service_id ==
  1303. WMI_CONTROL_SVC)
  1304. tx_resources =
  1305. HTC_MAX_MSG_PER_BUNDLE_TX;
  1306. else
  1307. tx_resources =
  1308. (HTC_MAX_MSG_PER_BUNDLE_TX * 2);
  1309. }
  1310. /* get all the packets for this endpoint that we can for
  1311. * this pass
  1312. */
  1313. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1314. tx_resources);
  1315. }
  1316. if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
  1317. /* didn't get any packets due to a lack of resources or
  1318. * TX queue was drained
  1319. */
  1320. break;
  1321. }
  1322. if (!pEndpoint->async_update)
  1323. UNLOCK_HTC_TX(target);
  1324. /* send what we can */
  1325. if (htc_issue_packets(target, pEndpoint, &sendQueue)) {
  1326. int i;
  1327. result = HTC_SEND_QUEUE_DROP;
  1328. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1329. ("htc_issue_packets, failed status:%d put it back to head of callersSendQueue",
  1330. result));
  1331. for (i = HTC_PACKET_QUEUE_DEPTH(&sendQueue); i > 0; i--)
  1332. hif_pm_runtime_put(target->hif_dev);
  1333. if (!pEndpoint->async_update) {
  1334. LOCK_HTC_TX(target);
  1335. }
  1336. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1337. &sendQueue);
  1338. break;
  1339. }
  1340. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1341. tx_resources =
  1342. hif_get_free_queue_number(target->hif_dev,
  1343. pEndpoint->UL_PipeID);
  1344. }
  1345. if (!pEndpoint->async_update) {
  1346. LOCK_HTC_TX(target);
  1347. }
  1348. }
  1349. /* done with this endpoint, we can clear the count */
  1350. qdf_atomic_init(&pEndpoint->TxProcessCount);
  1351. UNLOCK_HTC_TX(target);
  1352. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send:\n"));
  1353. return HTC_SEND_QUEUE_OK;
  1354. }
  1355. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1356. static uint16_t htc_send_pkts_sched_check(HTC_HANDLE HTCHandle,
  1357. HTC_ENDPOINT_ID id)
  1358. {
  1359. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1360. HTC_ENDPOINT *pEndpoint;
  1361. HTC_ENDPOINT_ID eid;
  1362. HTC_PACKET_QUEUE *pTxQueue;
  1363. uint16_t resources;
  1364. uint16_t acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
  1365. if (id < ENDPOINT_2 || id > ENDPOINT_5)
  1366. return 1;
  1367. for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
  1368. pEndpoint = &target->endpoint[eid];
  1369. pTxQueue = &pEndpoint->TxQueue;
  1370. if (HTC_QUEUE_EMPTY(pTxQueue))
  1371. acQueueStatus[eid - 2] = 1;
  1372. }
  1373. switch (id) {
  1374. case ENDPOINT_2: /* BE */
  1375. return acQueueStatus[0] && acQueueStatus[2]
  1376. && acQueueStatus[3];
  1377. case ENDPOINT_3: /* BK */
  1378. return acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
  1379. && acQueueStatus[3];
  1380. case ENDPOINT_4: /* VI */
  1381. return acQueueStatus[2] && acQueueStatus[3];
  1382. case ENDPOINT_5: /* VO */
  1383. return acQueueStatus[3];
  1384. default:
  1385. return 0;
  1386. }
  1387. }
  1388. static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
  1389. HTC_PACKET_QUEUE *pPktQueue,
  1390. HTC_ENDPOINT_ID eid)
  1391. {
  1392. HTC_ENDPOINT *pEndpoint;
  1393. HTC_PACKET_QUEUE *pTxQueue;
  1394. HTC_PACKET *pPacket;
  1395. int goodPkts;
  1396. pEndpoint = &target->endpoint[eid];
  1397. pTxQueue = &pEndpoint->TxQueue;
  1398. LOCK_HTC_TX(target);
  1399. goodPkts =
  1400. pEndpoint->MaxTxQueueDepth -
  1401. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  1402. if (goodPkts > 0) {
  1403. while (!HTC_QUEUE_EMPTY(pPktQueue)) {
  1404. pPacket = htc_packet_dequeue(pPktQueue);
  1405. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  1406. goodPkts--;
  1407. if (goodPkts <= 0)
  1408. break;
  1409. }
  1410. }
  1411. if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
  1412. ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
  1413. HTC_PACKET, ListLink) {
  1414. if (pEndpoint->EpCallBacks.
  1415. EpSendFull(pEndpoint->EpCallBacks.pContext,
  1416. pPacket) == HTC_SEND_FULL_DROP) {
  1417. INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
  1418. } else {
  1419. HTC_PACKET_REMOVE(pPktQueue, pPacket);
  1420. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  1421. }
  1422. }
  1423. ITERATE_END;
  1424. }
  1425. UNLOCK_HTC_TX(target);
  1426. return A_OK;
  1427. }
  1428. #endif
  1429. static inline QDF_STATUS __htc_send_pkt(HTC_HANDLE HTCHandle,
  1430. HTC_PACKET *pPacket)
  1431. {
  1432. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1433. HTC_ENDPOINT *pEndpoint;
  1434. HTC_PACKET_QUEUE pPktQueue;
  1435. qdf_nbuf_t netbuf;
  1436. HTC_FRAME_HDR *htc_hdr;
  1437. QDF_STATUS status;
  1438. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1439. ("+__htc_send_pkt\n"));
  1440. /* get packet at head to figure out which endpoint these packets will
  1441. * go into
  1442. */
  1443. if (!pPacket) {
  1444. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
  1445. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
  1446. return QDF_STATUS_E_INVAL;
  1447. }
  1448. if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
  1449. (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
  1450. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1451. ("%s endpoint is invalid\n", __func__));
  1452. AR_DEBUG_ASSERT(0);
  1453. return QDF_STATUS_E_INVAL;
  1454. }
  1455. pEndpoint = &target->endpoint[pPacket->Endpoint];
  1456. if (!pEndpoint->service_id) {
  1457. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
  1458. __func__));
  1459. return QDF_STATUS_E_INVAL;
  1460. }
  1461. #ifdef HTC_EP_STAT_PROFILING
  1462. LOCK_HTC_TX(target);
  1463. INC_HTC_EP_STAT(pEndpoint, TxPosted, 1);
  1464. UNLOCK_HTC_TX(target);
  1465. #endif
  1466. /* provide room in each packet's netbuf for the HTC frame header */
  1467. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1468. AR_DEBUG_ASSERT(netbuf);
  1469. if (!netbuf)
  1470. return QDF_STATUS_E_INVAL;
  1471. qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
  1472. pPacket->PktInfo.AsTx.Flags |=
  1473. HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
  1474. /* setup HTC frame header */
  1475. htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1476. AR_DEBUG_ASSERT(htc_hdr);
  1477. if (!htc_hdr)
  1478. return QDF_STATUS_E_INVAL;
  1479. HTC_WRITE32(htc_hdr,
  1480. SM(pPacket->ActualLength,
  1481. HTC_FRAME_HDR_PAYLOADLEN) |
  1482. SM(pPacket->Endpoint,
  1483. HTC_FRAME_HDR_ENDPOINTID));
  1484. LOCK_HTC_TX(target);
  1485. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1486. pEndpoint->SeqNo++;
  1487. HTC_WRITE32(((uint32_t *)htc_hdr) + 1,
  1488. SM(pPacket->PktInfo.AsTx.SeqNo,
  1489. HTC_FRAME_HDR_CONTROLBYTES1));
  1490. UNLOCK_HTC_TX(target);
  1491. /*
  1492. * For flow control enabled endpoints mapping is done in
  1493. * htc_issue_packets and for non flow control enabled endpoints
  1494. * its done here.
  1495. */
  1496. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1497. pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  1498. status = qdf_nbuf_map(target->osdev,
  1499. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  1500. QDF_DMA_TO_DEVICE);
  1501. if (status != QDF_STATUS_SUCCESS) {
  1502. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1503. ("%s: nbuf map failed, endpoint %pK, seq_no. %d\n",
  1504. __func__, pEndpoint, pEndpoint->SeqNo));
  1505. return status;
  1506. }
  1507. }
  1508. INIT_HTC_PACKET_QUEUE_AND_ADD(&pPktQueue, pPacket);
  1509. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1510. if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id))
  1511. htc_send_pkts_sched_queue(HTCHandle, &pPktQueue, pEndpoint->Id);
  1512. else
  1513. htc_try_send(target, pEndpoint, &pPktQueue);
  1514. #else
  1515. htc_try_send(target, pEndpoint, &pPktQueue);
  1516. #endif
  1517. /* do completion on any packets that couldn't get in */
  1518. while (!HTC_QUEUE_EMPTY(&pPktQueue)) {
  1519. pPacket = htc_packet_dequeue(&pPktQueue);
  1520. if (HTC_STOPPING(target))
  1521. pPacket->Status = QDF_STATUS_E_CANCELED;
  1522. else
  1523. pPacket->Status = QDF_STATUS_E_RESOURCES;
  1524. send_packet_completion(target, pPacket);
  1525. }
  1526. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
  1527. return QDF_STATUS_SUCCESS;
  1528. }
  1529. /* HTC API - htc_send_pkt */
  1530. QDF_STATUS htc_send_pkt(HTC_HANDLE htc_handle, HTC_PACKET *htc_packet)
  1531. {
  1532. if (!htc_handle) {
  1533. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1534. ("%s: HTCHandle is NULL \n", __func__));
  1535. return QDF_STATUS_E_FAILURE;
  1536. }
  1537. if (!htc_packet) {
  1538. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1539. ("%s: pPacket is NULL \n", __func__));
  1540. return QDF_STATUS_E_FAILURE;
  1541. }
  1542. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1543. ("+-htc_send_pkt: Enter endPointId: %d, buffer: %pK, length: %d\n",
  1544. htc_packet->Endpoint, htc_packet->pBuffer,
  1545. htc_packet->ActualLength));
  1546. return __htc_send_pkt(htc_handle, htc_packet);
  1547. }
  1548. qdf_export_symbol(htc_send_pkt);
  1549. #ifdef ATH_11AC_TXCOMPACT
  1550. /**
  1551. * htc_send_data_pkt() - send single data packet on an endpoint
  1552. * @HTCHandle: pointer to HTC handle
  1553. * @netbuf: network buffer containing the data to be sent
  1554. * @ActualLength: length of data that needs to be transmitted
  1555. *
  1556. * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
  1557. */
  1558. QDF_STATUS htc_send_data_pkt(HTC_HANDLE htc_hdl, qdf_nbuf_t netbuf, int ep_id,
  1559. int actual_length)
  1560. {
  1561. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
  1562. HTC_ENDPOINT *pEndpoint;
  1563. HTC_FRAME_HDR *p_htc_hdr;
  1564. QDF_STATUS status = QDF_STATUS_SUCCESS;
  1565. int tx_resources;
  1566. uint32_t data_attr = 0;
  1567. int htc_payload_len = actual_length;
  1568. pEndpoint = &target->endpoint[ep_id];
  1569. tx_resources = hif_get_free_queue_number(target->hif_dev,
  1570. pEndpoint->UL_PipeID);
  1571. if (tx_resources < HTC_DATA_RESOURCE_THRS) {
  1572. if (pEndpoint->ul_is_polled) {
  1573. hif_send_complete_check(pEndpoint->target->hif_dev,
  1574. pEndpoint->UL_PipeID, 1);
  1575. tx_resources =
  1576. hif_get_free_queue_number(target->hif_dev,
  1577. pEndpoint->UL_PipeID);
  1578. }
  1579. if (tx_resources < HTC_DATA_MINDESC_PERPACKET)
  1580. return QDF_STATUS_E_FAILURE;
  1581. }
  1582. if (hif_pm_runtime_get(target->hif_dev))
  1583. return QDF_STATUS_E_FAILURE;
  1584. p_htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1585. AR_DEBUG_ASSERT(p_htc_hdr);
  1586. data_attr = qdf_nbuf_data_attr_get(netbuf);
  1587. if (target->htc_hdr_length_check)
  1588. htc_payload_len = actual_length - HTC_HEADER_LEN;
  1589. HTC_WRITE32(p_htc_hdr, SM(htc_payload_len, HTC_FRAME_HDR_PAYLOADLEN)
  1590. | SM(ep_id, HTC_FRAME_HDR_ENDPOINTID));
  1591. /*
  1592. * If the HIF pipe for the data endpoint is polled rather than
  1593. * interrupt-driven, this is a good point to check whether any
  1594. * data previously sent through the HIF pipe have finished being
  1595. * sent.
  1596. * Since this may result in callbacks to htc_tx_completion_handler,
  1597. * which can take the HTC tx lock, make the hif_send_complete_check
  1598. * call before acquiring the HTC tx lock.
  1599. * Call hif_send_complete_check directly, rather than calling
  1600. * htc_send_complete_check, and call the PollTimerStart separately
  1601. * after calling hif_send_head, so the timer will be started to
  1602. * check for completion of the new outstanding download (in the
  1603. * unexpected event that other polling calls don't catch it).
  1604. */
  1605. LOCK_HTC_TX(target);
  1606. HTC_WRITE32(((uint32_t *)p_htc_hdr) + 1,
  1607. SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
  1608. pEndpoint->SeqNo++;
  1609. QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
  1610. DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1611. QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
  1612. sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
  1613. status = hif_send_head(target->hif_dev,
  1614. pEndpoint->UL_PipeID,
  1615. pEndpoint->Id, actual_length, netbuf, data_attr);
  1616. UNLOCK_HTC_TX(target);
  1617. return status;
  1618. }
  1619. #else /*ATH_11AC_TXCOMPACT */
  1620. /**
  1621. * htc_send_data_pkt() - htc_send_data_pkt
  1622. * @HTCHandle: pointer to HTC handle
  1623. * @pPacket: pointer to HTC_PACKET
  1624. * @more_data: indicates whether more data is to follow
  1625. *
  1626. * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
  1627. */
  1628. QDF_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
  1629. uint8_t more_data)
  1630. {
  1631. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1632. HTC_ENDPOINT *pEndpoint;
  1633. HTC_FRAME_HDR *pHtcHdr;
  1634. HTC_PACKET_QUEUE sendQueue;
  1635. qdf_nbuf_t netbuf = NULL;
  1636. int tx_resources;
  1637. QDF_STATUS status = QDF_STATUS_SUCCESS;
  1638. uint32_t data_attr = 0;
  1639. bool used_extra_tx_credit = false;
  1640. if (pPacket) {
  1641. if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
  1642. (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
  1643. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1644. ("%s endpoint is invalid\n", __func__));
  1645. AR_DEBUG_ASSERT(0);
  1646. return QDF_STATUS_E_INVAL;
  1647. }
  1648. pEndpoint = &target->endpoint[pPacket->Endpoint];
  1649. /* add HTC_FRAME_HDR in the initial fragment */
  1650. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1651. pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1652. AR_DEBUG_ASSERT(pHtcHdr);
  1653. HTC_WRITE32(pHtcHdr,
  1654. SM(pPacket->ActualLength,
  1655. HTC_FRAME_HDR_PAYLOADLEN) |
  1656. SM(pPacket->PktInfo.AsTx.SendFlags,
  1657. HTC_FRAME_HDR_FLAGS) |
  1658. SM(pPacket->Endpoint,
  1659. HTC_FRAME_HDR_ENDPOINTID));
  1660. /*
  1661. * If the HIF pipe for the data endpoint is polled rather than
  1662. * interrupt-driven, this is a good point to check whether any
  1663. * data previously sent through the HIF pipe have finished being
  1664. * sent. Since this may result in callbacks to
  1665. * htc_tx_completion_handler, which can take the HTC tx lock,
  1666. * make the hif_send_complete_check call before acquiring the
  1667. * HTC tx lock.
  1668. * Call hif_send_complete_check directly, rather than calling
  1669. * htc_send_complete_check, and call the PollTimerStart
  1670. * separately after calling hif_send_head, so the timer will be
  1671. * started to check for completion of the new outstanding
  1672. * download (in the unexpected event that other polling calls
  1673. * don't catch it).
  1674. */
  1675. if (pEndpoint->ul_is_polled) {
  1676. htc_send_complete_poll_timer_stop(pEndpoint);
  1677. hif_send_complete_check(pEndpoint->target->hif_dev,
  1678. pEndpoint->UL_PipeID, 0);
  1679. }
  1680. LOCK_HTC_TX(target);
  1681. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1682. pEndpoint->SeqNo++;
  1683. HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
  1684. SM(pPacket->PktInfo.AsTx.SeqNo,
  1685. HTC_FRAME_HDR_CONTROLBYTES1));
  1686. /* append new packet to pEndpoint->TxQueue */
  1687. HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
  1688. if (HTC_TX_BUNDLE_ENABLED(target) && (more_data)) {
  1689. UNLOCK_HTC_TX(target);
  1690. return QDF_STATUS_SUCCESS;
  1691. }
  1692. QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
  1693. DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1694. QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
  1695. sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
  1696. } else {
  1697. LOCK_HTC_TX(target);
  1698. pEndpoint = &target->endpoint[1];
  1699. }
  1700. /* increment tx processing count on entry */
  1701. qdf_atomic_inc(&pEndpoint->TxProcessCount);
  1702. if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  1703. /*
  1704. * Another thread or task is draining the TX queues on this
  1705. * endpoint. That thread will reset the tx processing count when
  1706. * the queue is drained.
  1707. */
  1708. qdf_atomic_dec(&pEndpoint->TxProcessCount);
  1709. UNLOCK_HTC_TX(target);
  1710. return QDF_STATUS_SUCCESS;
  1711. }
  1712. /***** beyond this point only 1 thread may enter ******/
  1713. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1714. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1715. #if DEBUG_CREDIT
  1716. int cred = pEndpoint->TxCredits;
  1717. #endif
  1718. get_htc_send_packets_credit_based(target, pEndpoint,
  1719. &sendQueue);
  1720. #if DEBUG_CREDIT
  1721. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1722. if (cred - pEndpoint->TxCredits > 0) {
  1723. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1724. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  1725. pEndpoint->Id, cred,
  1726. cred - pEndpoint->TxCredits,
  1727. pEndpoint->TxCredits));
  1728. }
  1729. }
  1730. #endif
  1731. UNLOCK_HTC_TX(target);
  1732. }
  1733. else if (HTC_TX_BUNDLE_ENABLED(target)) {
  1734. if (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB) {
  1735. if (hif_get_free_queue_number(target->hif_dev,
  1736. pEndpoint->UL_PipeID))
  1737. /*
  1738. * Header and payload belongs to the different
  1739. * fragments and consume 2 resource for one HTC
  1740. * package but USB combine into one transfer.
  1741. */
  1742. get_htc_send_packets(target, pEndpoint,
  1743. &sendQueue,
  1744. HTC_MAX_MSG_PER_BUNDLE_TX
  1745. * 2);
  1746. } else {
  1747. /* Dequeue max packets from endpoint tx queue */
  1748. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1749. HTC_MAX_TX_BUNDLE_SEND_LIMIT);
  1750. }
  1751. UNLOCK_HTC_TX(target);
  1752. } else {
  1753. /*
  1754. * Now drain the endpoint TX queue for transmission as long as
  1755. * we have enough transmit resources
  1756. */
  1757. tx_resources =
  1758. hif_get_free_queue_number(target->hif_dev,
  1759. pEndpoint->UL_PipeID);
  1760. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1761. tx_resources);
  1762. UNLOCK_HTC_TX(target);
  1763. }
  1764. /* send what we can */
  1765. while (true) {
  1766. if (HTC_TX_BUNDLE_ENABLED(target) &&
  1767. (HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
  1768. HTC_MIN_MSG_PER_BUNDLE) &&
  1769. (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO ||
  1770. hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)) {
  1771. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  1772. if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
  1773. status = QDF_STATUS_E_RESOURCES;
  1774. /* put the sendQueue back at the front
  1775. * of pEndpoint->TxQueue
  1776. */
  1777. LOCK_HTC_TX(target);
  1778. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(
  1779. &pEndpoint->TxQueue,
  1780. &sendQueue);
  1781. UNLOCK_HTC_TX(target);
  1782. break;
  1783. }
  1784. }
  1785. htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
  1786. }
  1787. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  1788. if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
  1789. status = QDF_STATUS_E_RESOURCES;
  1790. /* put the sendQueue back at the front
  1791. * of pEndpoint->TxQueue
  1792. */
  1793. LOCK_HTC_TX(target);
  1794. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(
  1795. &pEndpoint->TxQueue,
  1796. &sendQueue);
  1797. UNLOCK_HTC_TX(target);
  1798. break;
  1799. }
  1800. }
  1801. pPacket = htc_packet_dequeue(&sendQueue);
  1802. if (!pPacket)
  1803. break;
  1804. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1805. pHtcHdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1806. LOCK_HTC_TX(target);
  1807. /* store in look up queue to match completions */
  1808. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  1809. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  1810. pEndpoint->ul_outstanding_cnt++;
  1811. UNLOCK_HTC_TX(target);
  1812. used_extra_tx_credit =
  1813. htc_handle_extra_tx_credit(pEndpoint, pPacket,
  1814. (uint8_t *)pHtcHdr,
  1815. NULL,
  1816. pPacket->ActualLength
  1817. + HTC_HDR_LENGTH);
  1818. status = hif_send_head(target->hif_dev,
  1819. pEndpoint->UL_PipeID,
  1820. pEndpoint->Id,
  1821. HTC_HDR_LENGTH + pPacket->ActualLength,
  1822. netbuf, data_attr);
  1823. if (status != QDF_STATUS_SUCCESS) {
  1824. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  1825. if (used_extra_tx_credit) {
  1826. pEndpoint->EpCallBacks.
  1827. ep_padding_credit_update
  1828. (pEndpoint->EpCallBacks.pContext, 1);
  1829. }
  1830. }
  1831. }
  1832. #if DEBUG_BUNDLE
  1833. qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
  1834. pEndpoint->Id,
  1835. pEndpoint->TxCreditSize,
  1836. HTC_HDR_LENGTH + pPacket->ActualLength);
  1837. #endif
  1838. htc_issue_tx_bundle_stats_inc(target);
  1839. if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
  1840. LOCK_HTC_TX(target);
  1841. pEndpoint->ul_outstanding_cnt--;
  1842. /* remove this packet from the tx completion queue */
  1843. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  1844. /*
  1845. * Don't bother reclaiming credits - HTC flow control
  1846. * is not applicable to tx data.
  1847. * In LL systems, there is no download flow control,
  1848. * since there's virtually no download delay.
  1849. * In HL systems, the txrx SW explicitly performs the
  1850. * tx flow control.
  1851. */
  1852. /* pEndpoint->TxCredits +=
  1853. * pPacket->PktInfo.AsTx.CreditsUsed;
  1854. */
  1855. /* put this frame back at the front of the sendQueue */
  1856. HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
  1857. /* put the sendQueue back at the front of
  1858. * pEndpoint->TxQueue
  1859. */
  1860. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1861. &sendQueue);
  1862. UNLOCK_HTC_TX(target);
  1863. break; /* still need to reset TxProcessCount */
  1864. }
  1865. }
  1866. /* done with this endpoint, we can clear the count */
  1867. qdf_atomic_init(&pEndpoint->TxProcessCount);
  1868. if (pEndpoint->ul_is_polled) {
  1869. /*
  1870. * Start a cleanup timer to poll for download completion.
  1871. * The download completion should be noticed promptly from
  1872. * other polling calls, but the timer provides a safety net
  1873. * in case other polling calls don't occur as expected.
  1874. */
  1875. htc_send_complete_poll_timer_start(pEndpoint);
  1876. }
  1877. return status;
  1878. }
  1879. #endif /*ATH_11AC_TXCOMPACT */
  1880. qdf_export_symbol(htc_send_data_pkt);
  1881. /*
  1882. * In the adapted HIF layer, qdf_nbuf_t are passed between HIF and HTC,
  1883. * since upper layers expects HTC_PACKET containers we use the completed netbuf
  1884. * and lookup its corresponding HTC packet buffer from a lookup list.
  1885. * This is extra overhead that can be fixed by re-aligning HIF interfaces
  1886. * with HTC.
  1887. *
  1888. */
  1889. static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
  1890. HTC_ENDPOINT *pEndpoint,
  1891. qdf_nbuf_t netbuf)
  1892. {
  1893. HTC_PACKET *pPacket = NULL;
  1894. HTC_PACKET *pFoundPacket = NULL;
  1895. HTC_PACKET_QUEUE lookupQueue;
  1896. INIT_HTC_PACKET_QUEUE(&lookupQueue);
  1897. LOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  1898. LOCK_HTC_TX(target);
  1899. /* mark that HIF has indicated the send complete for another packet */
  1900. pEndpoint->ul_outstanding_cnt--;
  1901. /* Dequeue first packet directly because of in-order completion */
  1902. pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
  1903. if (qdf_unlikely(!pPacket)) {
  1904. UNLOCK_HTC_TX(target);
  1905. UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  1906. return NULL;
  1907. }
  1908. if (netbuf == (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  1909. UNLOCK_HTC_TX(target);
  1910. UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  1911. return pPacket;
  1912. }
  1913. HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
  1914. /*
  1915. * Move TX lookup queue to temp queue because most of packets that are
  1916. * not index 0 are not top 10 packets.
  1917. */
  1918. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
  1919. &pEndpoint->TxLookupQueue);
  1920. ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
  1921. HTC_PACKET, ListLink) {
  1922. if (!pPacket) {
  1923. pFoundPacket = pPacket;
  1924. break;
  1925. }
  1926. /* check for removal */
  1927. if (netbuf ==
  1928. (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  1929. /* found it */
  1930. HTC_PACKET_REMOVE(&lookupQueue, pPacket);
  1931. pFoundPacket = pPacket;
  1932. break;
  1933. }
  1934. }
  1935. ITERATE_END;
  1936. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
  1937. &lookupQueue);
  1938. UNLOCK_HTC_TX(target);
  1939. UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  1940. return pFoundPacket;
  1941. }
  1942. /**
  1943. * htc_tx_completion_handler() - htc tx completion handler
  1944. * @Context: pointer to HTC_TARGET structure
  1945. * @netbuf: pointer to netbuf for which completion handler is being called
  1946. * @EpID: end point Id on which the packet was sent
  1947. * @toeplitz_hash_result: toeplitz hash result
  1948. *
  1949. * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
  1950. */
  1951. QDF_STATUS htc_tx_completion_handler(void *Context,
  1952. qdf_nbuf_t netbuf, unsigned int EpID,
  1953. uint32_t toeplitz_hash_result)
  1954. {
  1955. HTC_TARGET *target = (HTC_TARGET *) Context;
  1956. HTC_ENDPOINT *pEndpoint;
  1957. HTC_PACKET *pPacket;
  1958. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1959. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5, ENDPOINT_4,
  1960. ENDPOINT_2, ENDPOINT_3 };
  1961. int epidIdx;
  1962. uint16_t resourcesThresh[DATA_EP_SIZE]; /* urb resources */
  1963. uint16_t resources;
  1964. uint16_t resourcesMax;
  1965. #endif
  1966. pEndpoint = &target->endpoint[EpID];
  1967. target->TX_comp_cnt++;
  1968. do {
  1969. pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
  1970. if (!pPacket) {
  1971. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1972. ("HTC TX lookup failed!\n"));
  1973. /* may have already been flushed and freed */
  1974. netbuf = NULL;
  1975. break;
  1976. }
  1977. if (pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_AUTO_PM)
  1978. hif_pm_runtime_put(target->hif_dev);
  1979. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
  1980. HTC_PACKET *pPacketTemp;
  1981. HTC_PACKET_QUEUE *pQueueSave =
  1982. (HTC_PACKET_QUEUE *) pPacket->pContext;
  1983. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
  1984. pPacketTemp) {
  1985. pPacket->Status = QDF_STATUS_SUCCESS;
  1986. send_packet_completion(target, pPacketTemp);
  1987. }
  1988. HTC_PACKET_QUEUE_ITERATE_END;
  1989. free_htc_bundle_packet(target, pPacket);
  1990. if (hif_get_bus_type(target->hif_dev) ==
  1991. QDF_BUS_TYPE_USB) {
  1992. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
  1993. htc_try_send(target, pEndpoint, NULL);
  1994. }
  1995. return QDF_STATUS_SUCCESS;
  1996. }
  1997. /* will be giving this buffer back to upper layers */
  1998. netbuf = NULL;
  1999. pPacket->Status = QDF_STATUS_SUCCESS;
  2000. send_packet_completion(target, pPacket);
  2001. } while (false);
  2002. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  2003. /* note: when using TX credit flow, the re-checking of queues
  2004. * happens when credits flow back from the target. In the non-TX
  2005. * credit case, we recheck after the packet completes
  2006. */
  2007. if ((qdf_atomic_read(&pEndpoint->TxProcessCount) == 0) ||
  2008. (!pEndpoint->async_update)) {
  2009. htc_try_send(target, pEndpoint, NULL);
  2010. }
  2011. }
  2012. return QDF_STATUS_SUCCESS;
  2013. }
  2014. #ifdef WLAN_FEATURE_FASTPATH
  2015. /**
  2016. * htc_ctrl_msg_cmpl(): checks for tx completion for the endpoint specified
  2017. * @HTC_HANDLE : pointer to the htc target context
  2018. * @htc_ep_id : end point id
  2019. *
  2020. * checks HTC tx completion
  2021. *
  2022. * Return: none
  2023. */
  2024. void htc_ctrl_msg_cmpl(HTC_HANDLE htc_pdev, HTC_ENDPOINT_ID htc_ep_id)
  2025. {
  2026. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_pdev);
  2027. HTC_ENDPOINT *pendpoint = &target->endpoint[htc_ep_id];
  2028. htc_send_complete_check(pendpoint, 1);
  2029. }
  2030. qdf_export_symbol(htc_ctrl_msg_cmpl);
  2031. #endif
  2032. /* callback when TX resources become available */
  2033. void htc_tx_resource_avail_handler(void *context, uint8_t pipeID)
  2034. {
  2035. int i;
  2036. HTC_TARGET *target = (HTC_TARGET *) context;
  2037. HTC_ENDPOINT *pEndpoint = NULL;
  2038. for (i = 0; i < ENDPOINT_MAX; i++) {
  2039. pEndpoint = &target->endpoint[i];
  2040. if (pEndpoint->service_id != 0) {
  2041. if (pEndpoint->UL_PipeID == pipeID)
  2042. break;
  2043. }
  2044. }
  2045. if (i >= ENDPOINT_MAX) {
  2046. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  2047. ("Invalid pipe indicated for TX resource avail : %d!\n",
  2048. pipeID));
  2049. return;
  2050. }
  2051. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  2052. ("HIF indicated more resources for pipe:%d\n",
  2053. pipeID));
  2054. htc_try_send(target, pEndpoint, NULL);
  2055. }
  2056. #ifdef FEATURE_RUNTIME_PM
  2057. /**
  2058. * htc_kick_queues(): resumes tx transactions of suspended endpoints
  2059. * @context: pointer to the htc target context
  2060. *
  2061. * Iterates through the enpoints and provides a context to empty queues
  2062. * int the hif layer when they are stalled due to runtime suspend.
  2063. *
  2064. * Return: none
  2065. */
  2066. void htc_kick_queues(void *context)
  2067. {
  2068. int i;
  2069. HTC_TARGET *target = (HTC_TARGET *)context;
  2070. HTC_ENDPOINT *endpoint = NULL;
  2071. for (i = 0; i < ENDPOINT_MAX; i++) {
  2072. endpoint = &target->endpoint[i];
  2073. if (endpoint->service_id == 0)
  2074. continue;
  2075. if (endpoint->EpCallBacks.ep_resume_tx_queue)
  2076. endpoint->EpCallBacks.ep_resume_tx_queue(
  2077. endpoint->EpCallBacks.pContext);
  2078. htc_try_send(target, endpoint, NULL);
  2079. }
  2080. hif_fastpath_resume(target->hif_dev);
  2081. }
  2082. #endif
  2083. /* flush endpoint TX queue */
  2084. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  2085. HTC_TX_TAG Tag)
  2086. {
  2087. HTC_PACKET *pPacket;
  2088. LOCK_HTC_TX(target);
  2089. while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  2090. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  2091. if (pPacket) {
  2092. /* let the sender know the packet was not delivered */
  2093. pPacket->Status = QDF_STATUS_E_CANCELED;
  2094. send_packet_completion(target, pPacket);
  2095. }
  2096. }
  2097. UNLOCK_HTC_TX(target);
  2098. }
  2099. /* flush pending entries in endpoint TX Lookup queue */
  2100. void htc_flush_endpoint_txlookupQ(HTC_TARGET *target,
  2101. HTC_ENDPOINT_ID endpoint_id,
  2102. bool call_ep_callback)
  2103. {
  2104. HTC_PACKET *packet;
  2105. HTC_ENDPOINT *endpoint;
  2106. endpoint = &target->endpoint[endpoint_id];
  2107. if (!endpoint && endpoint->service_id == 0)
  2108. return;
  2109. LOCK_HTC_TX(target);
  2110. while (HTC_PACKET_QUEUE_DEPTH(&endpoint->TxLookupQueue)) {
  2111. packet = htc_packet_dequeue(&endpoint->TxLookupQueue);
  2112. if (packet) {
  2113. if (call_ep_callback == true) {
  2114. packet->Status = QDF_STATUS_E_CANCELED;
  2115. send_packet_completion(target, packet);
  2116. } else {
  2117. qdf_mem_free(packet);
  2118. }
  2119. }
  2120. }
  2121. UNLOCK_HTC_TX(target);
  2122. }
  2123. /* HTC API to flush an endpoint's TX queue*/
  2124. void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
  2125. HTC_TX_TAG Tag)
  2126. {
  2127. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  2128. HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
  2129. if (pEndpoint->service_id == 0) {
  2130. AR_DEBUG_ASSERT(false);
  2131. /* not in use.. */
  2132. return;
  2133. }
  2134. htc_flush_endpoint_tx(target, pEndpoint, Tag);
  2135. }
  2136. /* HTC API to indicate activity to the credit distribution function */
  2137. void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
  2138. HTC_ENDPOINT_ID Endpoint, bool Active)
  2139. {
  2140. /* TODO */
  2141. }
  2142. bool htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
  2143. {
  2144. return true;
  2145. }
  2146. void htc_set_nodrop_pkt(HTC_HANDLE HTCHandle, A_BOOL isNodropPkt)
  2147. {
  2148. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  2149. target->is_nodrop_pkt = isNodropPkt;
  2150. }
  2151. void htc_enable_hdr_length_check(HTC_HANDLE htc_hdl, bool htc_hdr_length_check)
  2152. {
  2153. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
  2154. target->htc_hdr_length_check = htc_hdr_length_check;
  2155. }
  2156. /**
  2157. * htc_process_credit_rpt() - process credit report, call distribution function
  2158. * @target: pointer to HTC_TARGET
  2159. * @pRpt: pointer to HTC_CREDIT_REPORT
  2160. * @NumEntries: number of entries in credit report
  2161. * @FromEndpoint: endpoint for which credit report is received
  2162. *
  2163. * Return: A_OK for success or an appropriate A_STATUS error
  2164. */
  2165. void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
  2166. int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
  2167. {
  2168. int i;
  2169. HTC_ENDPOINT *pEndpoint;
  2170. int totalCredits = 0;
  2171. uint8_t rpt_credits, rpt_ep_id;
  2172. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  2173. ("+htc_process_credit_rpt, Credit Report Entries:%d\n",
  2174. NumEntries));
  2175. /* lock out TX while we update credits */
  2176. LOCK_HTC_TX(target);
  2177. for (i = 0; i < NumEntries; i++, pRpt++) {
  2178. rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
  2179. if (rpt_ep_id >= ENDPOINT_MAX) {
  2180. AR_DEBUG_ASSERT(false);
  2181. break;
  2182. }
  2183. rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
  2184. pEndpoint = &target->endpoint[rpt_ep_id];
  2185. #if DEBUG_CREDIT
  2186. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  2187. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  2188. (" <HTC> Increase EP%d %d + %d = %d credits\n",
  2189. rpt_ep_id, pEndpoint->TxCredits,
  2190. rpt_credits,
  2191. pEndpoint->TxCredits + rpt_credits));
  2192. }
  2193. #endif
  2194. #ifdef HTC_EP_STAT_PROFILING
  2195. INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
  2196. INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
  2197. if (FromEndpoint == rpt_ep_id) {
  2198. /* this credit report arrived on the same endpoint
  2199. * indicating it arrived in an RX packet
  2200. */
  2201. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
  2202. rpt_credits);
  2203. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
  2204. } else if (FromEndpoint == ENDPOINT_0) {
  2205. /* this credit arrived on endpoint 0 as a NULL msg */
  2206. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
  2207. rpt_credits);
  2208. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
  2209. } else {
  2210. /* arrived on another endpoint */
  2211. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
  2212. rpt_credits);
  2213. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
  2214. }
  2215. #endif
  2216. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  2217. htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
  2218. pEndpoint->TxCredits + rpt_credits,
  2219. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  2220. TxQueue));
  2221. }
  2222. pEndpoint->TxCredits += rpt_credits;
  2223. if (pEndpoint->TxCredits
  2224. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  2225. UNLOCK_HTC_TX(target);
  2226. #ifdef ATH_11AC_TXCOMPACT
  2227. htc_try_send(target, pEndpoint, NULL);
  2228. #else
  2229. if (pEndpoint->service_id == HTT_DATA_MSG_SVC)
  2230. htc_send_data_pkt((HTC_HANDLE)target, NULL, 0);
  2231. else
  2232. htc_try_send(target, pEndpoint, NULL);
  2233. #endif
  2234. LOCK_HTC_TX(target);
  2235. }
  2236. totalCredits += rpt_credits;
  2237. }
  2238. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  2239. (" Report indicated %d credits to distribute\n",
  2240. totalCredits));
  2241. UNLOCK_HTC_TX(target);
  2242. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt\n"));
  2243. }
  2244. /* function to fetch stats from htc layer*/
  2245. struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
  2246. {
  2247. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  2248. return &(target->htc_pkt_stats);
  2249. }