htc_send.c 83 KB

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