htc_send.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041
  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. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  1075. pPacket = htc_get_pkt_at_head(tx_queue);
  1076. if (!pPacket)
  1077. break;
  1078. log_packet_info(target, pPacket);
  1079. break;
  1080. }
  1081. htc_inc_wmi_runtime_cnt(target, rtpm_code);
  1082. }
  1083. sendFlags = 0;
  1084. /* get packet at head, but don't remove it */
  1085. pPacket = htc_get_pkt_at_head(tx_queue);
  1086. if (!pPacket) {
  1087. if (do_pm_get) {
  1088. hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
  1089. htc_dec_wmi_runtime_cnt(target, rtpm_code);
  1090. }
  1091. break;
  1092. }
  1093. if (sys_pm_check &&
  1094. hif_system_pm_state_check(target->hif_dev)) {
  1095. if (do_pm_get) {
  1096. hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
  1097. htc_dec_wmi_runtime_cnt(target, rtpm_code);
  1098. }
  1099. break;
  1100. }
  1101. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1102. (" Got head packet:%pK , Queue Depth: %d\n",
  1103. pPacket,
  1104. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  1105. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  1106. if (transferLength <= pEndpoint->TxCreditSize) {
  1107. creditsRequired = 1;
  1108. } else {
  1109. /* figure out how many credits this message requires */
  1110. creditsRequired =
  1111. transferLength / pEndpoint->TxCreditSize;
  1112. remainder = transferLength % pEndpoint->TxCreditSize;
  1113. if (remainder)
  1114. creditsRequired++;
  1115. }
  1116. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1117. (" Credits Required:%d Got:%d\n",
  1118. creditsRequired, pEndpoint->TxCredits));
  1119. if (pEndpoint->Id == ENDPOINT_0) {
  1120. /*
  1121. * endpoint 0 is special, it always has a credit and
  1122. * does not require credit based flow control
  1123. */
  1124. creditsRequired = 0;
  1125. } else {
  1126. if (pEndpoint->TxCredits < creditsRequired) {
  1127. #if DEBUG_CREDIT
  1128. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1129. ("EP%d,No Credit now.%d < %d\n",
  1130. pEndpoint->Id,
  1131. pEndpoint->TxCredits,
  1132. creditsRequired));
  1133. #endif
  1134. if (do_pm_get) {
  1135. hif_rtpm_put(HIF_RTPM_PUT_ASYNC,
  1136. rtpm_code);
  1137. htc_dec_wmi_runtime_cnt(target,
  1138. rtpm_code);
  1139. }
  1140. break;
  1141. }
  1142. pEndpoint->TxCredits -= creditsRequired;
  1143. INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
  1144. creditsRequired);
  1145. /* check if we need credits back from the target */
  1146. if (pEndpoint->TxCredits <=
  1147. pEndpoint->TxCreditsPerMaxMsg) {
  1148. /* tell the target we need credits ASAP! */
  1149. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  1150. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  1151. htc_credit_record(HTC_REQUEST_CREDIT,
  1152. pEndpoint->TxCredits,
  1153. HTC_PACKET_QUEUE_DEPTH
  1154. (tx_queue));
  1155. hif_latency_detect_credit_record_time(
  1156. HIF_REQUEST_CREDIT,
  1157. target->hif_dev);
  1158. }
  1159. INC_HTC_EP_STAT(pEndpoint,
  1160. TxCreditLowIndications, 1);
  1161. #if DEBUG_CREDIT
  1162. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1163. (" EP%d Needs Credits\n",
  1164. pEndpoint->Id));
  1165. #endif
  1166. }
  1167. }
  1168. /* now we can fully dequeue */
  1169. pPacket = htc_packet_dequeue(tx_queue);
  1170. if (pPacket) {
  1171. /* save the number of credits this packet consumed */
  1172. pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
  1173. /* save send flags */
  1174. pPacket->PktInfo.AsTx.SendFlags = sendFlags;
  1175. /* queue this packet into the caller's queue */
  1176. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  1177. }
  1178. }
  1179. if (!HTC_QUEUE_EMPTY(&pm_queue))
  1180. queue_htc_pm_packets(pEndpoint, &pm_queue);
  1181. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1182. ("-get_htc_send_packets_credit_based\n"));
  1183. }
  1184. static void get_htc_send_packets(HTC_TARGET *target,
  1185. HTC_ENDPOINT *pEndpoint,
  1186. HTC_PACKET_QUEUE *pQueue, int Resources)
  1187. {
  1188. HTC_PACKET *pPacket;
  1189. HTC_PACKET_QUEUE *tx_queue;
  1190. HTC_PACKET_QUEUE pm_queue;
  1191. bool do_pm_get = false;
  1192. unsigned int rtpm_code = 0;
  1193. int ret;
  1194. /*** NOTE : the TX lock is held when this function is called ***/
  1195. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1196. ("+get_htc_send_packets %d resources\n", Resources));
  1197. INIT_HTC_PACKET_QUEUE(&pm_queue);
  1198. extract_htc_pm_packets(pEndpoint, &pm_queue);
  1199. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  1200. tx_queue = &pEndpoint->TxQueue;
  1201. do_pm_get = true;
  1202. } else {
  1203. tx_queue = &pm_queue;
  1204. }
  1205. /* loop until we can grab as many packets out of the queue as we can */
  1206. while (Resources > 0) {
  1207. int num_frags;
  1208. if (do_pm_get) {
  1209. rtpm_code =
  1210. htc_send_pkts_get_rtpm_id(
  1211. pEndpoint->service_id);
  1212. ret = hif_rtpm_get(HIF_RTPM_GET_ASYNC, rtpm_code);
  1213. if (ret) {
  1214. /* bus suspended, runtime resume issued */
  1215. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  1216. pPacket = htc_get_pkt_at_head(tx_queue);
  1217. if (!pPacket)
  1218. break;
  1219. log_packet_info(target, pPacket);
  1220. break;
  1221. }
  1222. htc_inc_wmi_runtime_cnt(target, rtpm_code);
  1223. }
  1224. ret = hif_system_pm_state_check(target->hif_dev);
  1225. if (ret) {
  1226. if (do_pm_get) {
  1227. hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
  1228. htc_dec_wmi_runtime_cnt(target, rtpm_code);
  1229. }
  1230. break;
  1231. }
  1232. pPacket = htc_packet_dequeue(tx_queue);
  1233. if (!pPacket) {
  1234. if (do_pm_get) {
  1235. hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
  1236. htc_dec_wmi_runtime_cnt(target, rtpm_code);
  1237. }
  1238. break;
  1239. }
  1240. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1241. (" Got packet:%pK , New Queue Depth: %d\n",
  1242. pPacket,
  1243. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  1244. /* For non-credit path the sequence number is already embedded
  1245. * in the constructed HTC header
  1246. */
  1247. pPacket->PktInfo.AsTx.SendFlags = 0;
  1248. pPacket->PktInfo.AsTx.CreditsUsed = 0;
  1249. /* queue this packet into the caller's queue */
  1250. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  1251. /*
  1252. * FIX THIS:
  1253. * For now, avoid calling qdf_nbuf_get_num_frags before calling
  1254. * qdf_nbuf_map, because the MacOS version of qdf_nbuf_t doesn't
  1255. * support qdf_nbuf_get_num_frags until after qdf_nbuf_map has
  1256. * been done.
  1257. * Assume that the non-data netbufs, i.e. WMI message netbufs,
  1258. * consist of a single fragment.
  1259. */
  1260. /* WMI messages are in a single-fragment network buf */
  1261. num_frags =
  1262. (pPacket->PktInfo.AsTx.
  1263. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1 :
  1264. qdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
  1265. (pPacket));
  1266. Resources -= num_frags;
  1267. }
  1268. if (!HTC_QUEUE_EMPTY(&pm_queue))
  1269. queue_htc_pm_packets(pEndpoint, &pm_queue);
  1270. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets\n"));
  1271. }
  1272. /**
  1273. * htc_try_send() - Send packets in a queue on an endpoint
  1274. * @target: HTC target on which packets need to be sent
  1275. * @pEndpoint: logical endpoint on which packets needs to be sent
  1276. * @pCallersSendQueue: packet queue containing the list of packets to be sent
  1277. *
  1278. * Return: enum HTC_SEND_QUEUE_RESULT indicates whether the packet was queued to
  1279. * be sent or the packet should be dropped by the upper layer
  1280. */
  1281. static enum HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
  1282. HTC_ENDPOINT *pEndpoint,
  1283. HTC_PACKET_QUEUE *pCallersSendQueue)
  1284. {
  1285. /* temp queue to hold packets at various stages */
  1286. HTC_PACKET_QUEUE sendQueue;
  1287. HTC_PACKET *pPacket;
  1288. int tx_resources;
  1289. int overflow;
  1290. enum HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
  1291. QDF_STATUS status;
  1292. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%pK Depth:%d)\n",
  1293. pCallersSendQueue,
  1294. (pCallersSendQueue ==
  1295. NULL) ? 0 :
  1296. HTC_PACKET_QUEUE_DEPTH
  1297. (pCallersSendQueue)));
  1298. /* init the local send queue */
  1299. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1300. do {
  1301. /* caller didn't provide a queue, just wants us to check
  1302. * queues and send
  1303. */
  1304. if (!pCallersSendQueue)
  1305. break;
  1306. if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
  1307. /* empty queue */
  1308. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  1309. HTC_PKT_Q_EMPTY);
  1310. result = HTC_SEND_QUEUE_DROP;
  1311. break;
  1312. }
  1313. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
  1314. pEndpoint->MaxTxQueueDepth) {
  1315. /* we've already overflowed */
  1316. overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  1317. } else {
  1318. /* figure out how much we will overflow by */
  1319. overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  1320. overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  1321. /* get how much we will overflow the TX queue by */
  1322. overflow -= pEndpoint->MaxTxQueueDepth;
  1323. }
  1324. /* if overflow is negative or zero, we are okay */
  1325. if (overflow > 0) {
  1326. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1327. ("Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d\n",
  1328. pEndpoint->Id, overflow,
  1329. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  1330. TxQueue),
  1331. pEndpoint->MaxTxQueueDepth));
  1332. }
  1333. if ((overflow <= 0)
  1334. || (!pEndpoint->EpCallBacks.EpSendFull)) {
  1335. /* all packets will fit or caller did not provide send
  1336. * full indication handler
  1337. * just move all of them to local sendQueue object
  1338. */
  1339. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
  1340. pCallersSendQueue);
  1341. } else {
  1342. int i;
  1343. int goodPkts =
  1344. HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
  1345. overflow;
  1346. A_ASSERT(goodPkts >= 0);
  1347. /* we have overflowed and callback is provided. Dequeue
  1348. * all non-overflow packets into the sendqueue
  1349. */
  1350. for (i = 0; i < goodPkts; i++) {
  1351. /* pop off caller's queue */
  1352. pPacket = htc_packet_dequeue(pCallersSendQueue);
  1353. A_ASSERT(pPacket);
  1354. if (pPacket)
  1355. /* insert into local queue */
  1356. HTC_PACKET_ENQUEUE(&sendQueue,
  1357. pPacket);
  1358. }
  1359. /* the caller's queue has all the packets that won't fit
  1360. * walk through the caller's queue and indicate each one
  1361. * to the send full handler
  1362. */
  1363. ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
  1364. QueueHead, pPacket,
  1365. HTC_PACKET, ListLink) {
  1366. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1367. ("Indicating overflowed TX packet: %pK\n",
  1368. pPacket));
  1369. /*
  1370. * Remove headroom reserved for HTC_FRAME_HDR
  1371. * before giving the packet back to the user via
  1372. * the EpSendFull callback.
  1373. */
  1374. qdf_nbuf_pull_head
  1375. (GET_HTC_PACKET_NET_BUF_CONTEXT
  1376. (pPacket),
  1377. sizeof(HTC_FRAME_HDR));
  1378. pPacket->PktInfo.AsTx.Flags &=
  1379. ~HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
  1380. if (pEndpoint->EpCallBacks.
  1381. EpSendFull(pEndpoint->EpCallBacks.pContext,
  1382. pPacket) == HTC_SEND_FULL_DROP) {
  1383. /* callback wants the packet dropped */
  1384. INC_HTC_EP_STAT(pEndpoint, TxDropped,
  1385. 1);
  1386. /* leave this one in the caller's queue
  1387. * for cleanup
  1388. */
  1389. } else {
  1390. /* callback wants to keep this packet,
  1391. * remove from caller's queue
  1392. */
  1393. HTC_PACKET_REMOVE(pCallersSendQueue,
  1394. pPacket);
  1395. /* put it in the send queue
  1396. * add HTC_FRAME_HDR space reservation
  1397. * again
  1398. */
  1399. qdf_nbuf_push_head
  1400. (GET_HTC_PACKET_NET_BUF_CONTEXT
  1401. (pPacket),
  1402. sizeof(HTC_FRAME_HDR));
  1403. pPacket->PktInfo.AsTx.Flags |=
  1404. HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
  1405. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  1406. }
  1407. }
  1408. ITERATE_END;
  1409. if (HTC_QUEUE_EMPTY(&sendQueue)) {
  1410. /* no packets made it in, caller will cleanup */
  1411. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  1412. HTC_SEND_Q_EMPTY);
  1413. result = HTC_SEND_QUEUE_DROP;
  1414. break;
  1415. }
  1416. }
  1417. } while (false);
  1418. if (result != HTC_SEND_QUEUE_OK) {
  1419. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: %d\n",
  1420. result));
  1421. return result;
  1422. }
  1423. LOCK_HTC_TX(target);
  1424. if (!HTC_QUEUE_EMPTY(&sendQueue)) {
  1425. if (target->is_nodrop_pkt) {
  1426. /*
  1427. * nodrop pkts have higher priority than normal pkts,
  1428. * insert nodrop pkt to head for proper
  1429. * start/termination of test.
  1430. */
  1431. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1432. &sendQueue);
  1433. target->is_nodrop_pkt = false;
  1434. } else {
  1435. /* transfer packets to tail */
  1436. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
  1437. &sendQueue);
  1438. A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
  1439. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1440. }
  1441. }
  1442. /* increment tx processing count on entry */
  1443. if (qdf_atomic_inc_return(&pEndpoint->TxProcessCount) > 1) {
  1444. /* another thread or task is draining the TX queues on this
  1445. * endpoint that thread will reset the tx processing count when
  1446. * the queue is drained
  1447. */
  1448. qdf_atomic_dec(&pEndpoint->TxProcessCount);
  1449. UNLOCK_HTC_TX(target);
  1450. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy)\n"));
  1451. return HTC_SEND_QUEUE_OK;
  1452. }
  1453. /***** beyond this point only 1 thread may enter ******/
  1454. /* now drain the endpoint TX queue for transmission as long as we have
  1455. * enough transmit resources
  1456. */
  1457. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1458. tx_resources =
  1459. hif_get_free_queue_number(target->hif_dev,
  1460. pEndpoint->UL_PipeID);
  1461. } else {
  1462. tx_resources = 0;
  1463. }
  1464. while (true) {
  1465. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0)
  1466. break;
  1467. if (pEndpoint->async_update &&
  1468. (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) &&
  1469. (!tx_resources)) {
  1470. hif_schedule_ce_tasklet(target->hif_dev,
  1471. pEndpoint->UL_PipeID);
  1472. break;
  1473. }
  1474. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1475. #if DEBUG_CREDIT
  1476. int cred = pEndpoint->TxCredits;
  1477. #endif
  1478. /* credit based mechanism provides flow control based on
  1479. * target transmit resource availability, we assume that
  1480. * the HIF layer will always have bus resources greater
  1481. * than target transmit resources
  1482. */
  1483. get_htc_send_packets_credit_based(target, pEndpoint,
  1484. &sendQueue);
  1485. #if DEBUG_CREDIT
  1486. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1487. if (cred - pEndpoint->TxCredits > 0) {
  1488. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1489. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  1490. pEndpoint->Id, cred,
  1491. cred -
  1492. pEndpoint->TxCredits,
  1493. pEndpoint->TxCredits));
  1494. }
  1495. }
  1496. #endif
  1497. } else {
  1498. /*
  1499. * Header and payload belongs to the different fragments and
  1500. * consume 2 resource for one HTC package but USB combine into
  1501. * one transfer.And one WMI message only consumes one single
  1502. * resource.
  1503. */
  1504. if (HTC_TX_BUNDLE_ENABLED(target) && tx_resources &&
  1505. hif_get_bus_type(target->hif_dev) ==
  1506. QDF_BUS_TYPE_USB) {
  1507. if (pEndpoint->service_id ==
  1508. WMI_CONTROL_SVC)
  1509. tx_resources =
  1510. HTC_MAX_MSG_PER_BUNDLE_TX;
  1511. else
  1512. tx_resources =
  1513. (HTC_MAX_MSG_PER_BUNDLE_TX * 2);
  1514. }
  1515. /* get all the packets for this endpoint that we can for
  1516. * this pass
  1517. */
  1518. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1519. tx_resources);
  1520. }
  1521. if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
  1522. /* didn't get any packets due to a lack of resources or
  1523. * TX queue was drained
  1524. */
  1525. break;
  1526. }
  1527. if (!pEndpoint->async_update)
  1528. UNLOCK_HTC_TX(target);
  1529. /* send what we can */
  1530. status = htc_issue_packets(target, pEndpoint, &sendQueue);
  1531. if (status) {
  1532. int i;
  1533. unsigned int rtpm_code;
  1534. result = HTC_SEND_QUEUE_DROP;
  1535. switch (status) {
  1536. case QDF_STATUS_E_RESOURCES:
  1537. if (pEndpoint->num_requeues_warn <= MAX_REQUEUE_WARN) {
  1538. pEndpoint->num_requeues_warn++;
  1539. pEndpoint->total_num_requeues++;
  1540. break;
  1541. } else {
  1542. pEndpoint->total_num_requeues++;
  1543. pEndpoint->num_requeues_warn = 0;
  1544. }
  1545. fallthrough;
  1546. default:
  1547. QDF_TRACE(QDF_MODULE_ID_HIF, QDF_TRACE_LEVEL_INFO,
  1548. "htc_issue_packets, failed status:%d"
  1549. "endpoint:%d, put it back to head of"
  1550. "callersSendQueue", result, pEndpoint->Id);
  1551. break;
  1552. }
  1553. rtpm_code = htc_send_pkts_get_rtpm_id(
  1554. pEndpoint->service_id);
  1555. for (i = HTC_PACKET_QUEUE_DEPTH(&sendQueue); i > 0; i--) {
  1556. hif_rtpm_put(HIF_RTPM_PUT_ASYNC, rtpm_code);
  1557. htc_dec_wmi_runtime_cnt(target, rtpm_code);
  1558. }
  1559. if (!pEndpoint->async_update) {
  1560. LOCK_HTC_TX(target);
  1561. }
  1562. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1563. &sendQueue);
  1564. break;
  1565. } else {
  1566. if (pEndpoint->num_requeues_warn)
  1567. pEndpoint->num_requeues_warn = 0;
  1568. }
  1569. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1570. tx_resources =
  1571. hif_get_free_queue_number(target->hif_dev,
  1572. pEndpoint->UL_PipeID);
  1573. }
  1574. if (!pEndpoint->async_update) {
  1575. LOCK_HTC_TX(target);
  1576. }
  1577. }
  1578. /* done with this endpoint, we can clear the count */
  1579. qdf_atomic_init(&pEndpoint->TxProcessCount);
  1580. UNLOCK_HTC_TX(target);
  1581. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send:\n"));
  1582. return HTC_SEND_QUEUE_OK;
  1583. }
  1584. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1585. static uint16_t htc_send_pkts_sched_check(HTC_HANDLE HTCHandle,
  1586. HTC_ENDPOINT_ID id)
  1587. {
  1588. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1589. HTC_ENDPOINT *pEndpoint;
  1590. HTC_ENDPOINT_ID eid;
  1591. HTC_PACKET_QUEUE *pTxQueue;
  1592. uint16_t resources;
  1593. uint16_t acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
  1594. if (id < ENDPOINT_2 || id > ENDPOINT_5)
  1595. return 1;
  1596. for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
  1597. pEndpoint = &target->endpoint[eid];
  1598. pTxQueue = &pEndpoint->TxQueue;
  1599. if (HTC_QUEUE_EMPTY(pTxQueue))
  1600. acQueueStatus[eid - 2] = 1;
  1601. }
  1602. switch (id) {
  1603. case ENDPOINT_2: /* BE */
  1604. return acQueueStatus[0] && acQueueStatus[2]
  1605. && acQueueStatus[3];
  1606. case ENDPOINT_3: /* BK */
  1607. return acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
  1608. && acQueueStatus[3];
  1609. case ENDPOINT_4: /* VI */
  1610. return acQueueStatus[2] && acQueueStatus[3];
  1611. case ENDPOINT_5: /* VO */
  1612. return acQueueStatus[3];
  1613. default:
  1614. return 0;
  1615. }
  1616. }
  1617. static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
  1618. HTC_PACKET_QUEUE *pPktQueue,
  1619. HTC_ENDPOINT_ID eid)
  1620. {
  1621. HTC_ENDPOINT *pEndpoint;
  1622. HTC_PACKET_QUEUE *pTxQueue;
  1623. HTC_PACKET *pPacket;
  1624. int goodPkts;
  1625. pEndpoint = &target->endpoint[eid];
  1626. pTxQueue = &pEndpoint->TxQueue;
  1627. LOCK_HTC_TX(target);
  1628. goodPkts =
  1629. pEndpoint->MaxTxQueueDepth -
  1630. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  1631. if (goodPkts > 0) {
  1632. while (!HTC_QUEUE_EMPTY(pPktQueue)) {
  1633. pPacket = htc_packet_dequeue(pPktQueue);
  1634. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  1635. goodPkts--;
  1636. if (goodPkts <= 0)
  1637. break;
  1638. }
  1639. }
  1640. if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
  1641. ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
  1642. HTC_PACKET, ListLink) {
  1643. if (pEndpoint->EpCallBacks.
  1644. EpSendFull(pEndpoint->EpCallBacks.pContext,
  1645. pPacket) == HTC_SEND_FULL_DROP) {
  1646. INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
  1647. } else {
  1648. HTC_PACKET_REMOVE(pPktQueue, pPacket);
  1649. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  1650. }
  1651. }
  1652. ITERATE_END;
  1653. }
  1654. UNLOCK_HTC_TX(target);
  1655. return A_OK;
  1656. }
  1657. #endif
  1658. static inline QDF_STATUS __htc_send_pkt(HTC_HANDLE HTCHandle,
  1659. HTC_PACKET *pPacket)
  1660. {
  1661. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1662. HTC_ENDPOINT *pEndpoint;
  1663. HTC_PACKET_QUEUE pPktQueue;
  1664. qdf_nbuf_t netbuf;
  1665. HTC_FRAME_HDR *htc_hdr;
  1666. QDF_STATUS status;
  1667. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1668. ("+__htc_send_pkt\n"));
  1669. /* get packet at head to figure out which endpoint these packets will
  1670. * go into
  1671. */
  1672. if (!pPacket) {
  1673. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
  1674. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
  1675. return QDF_STATUS_E_INVAL;
  1676. }
  1677. if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
  1678. (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
  1679. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1680. ("%s endpoint is invalid\n", __func__));
  1681. AR_DEBUG_ASSERT(0);
  1682. return QDF_STATUS_E_INVAL;
  1683. }
  1684. pEndpoint = &target->endpoint[pPacket->Endpoint];
  1685. if (!pEndpoint->service_id) {
  1686. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
  1687. __func__));
  1688. return QDF_STATUS_E_INVAL;
  1689. }
  1690. #ifdef HTC_EP_STAT_PROFILING
  1691. LOCK_HTC_TX(target);
  1692. INC_HTC_EP_STAT(pEndpoint, TxPosted, 1);
  1693. UNLOCK_HTC_TX(target);
  1694. #endif
  1695. /* provide room in each packet's netbuf for the HTC frame header */
  1696. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1697. AR_DEBUG_ASSERT(netbuf);
  1698. if (!netbuf)
  1699. return QDF_STATUS_E_INVAL;
  1700. qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
  1701. pPacket->PktInfo.AsTx.Flags |=
  1702. HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
  1703. /* setup HTC frame header */
  1704. htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1705. AR_DEBUG_ASSERT(htc_hdr);
  1706. if (!htc_hdr)
  1707. return QDF_STATUS_E_INVAL;
  1708. HTC_WRITE32(htc_hdr,
  1709. SM(pPacket->ActualLength,
  1710. HTC_FRAME_HDR_PAYLOADLEN) |
  1711. SM(pPacket->Endpoint,
  1712. HTC_FRAME_HDR_ENDPOINTID));
  1713. LOCK_HTC_TX(target);
  1714. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1715. pEndpoint->SeqNo++;
  1716. HTC_WRITE32(((uint32_t *)htc_hdr) + 1,
  1717. SM(pPacket->PktInfo.AsTx.SeqNo,
  1718. HTC_FRAME_HDR_CONTROLBYTES1));
  1719. UNLOCK_HTC_TX(target);
  1720. /*
  1721. * For flow control enabled endpoints mapping is done in
  1722. * htc_issue_packets and for non flow control enabled endpoints
  1723. * its done here.
  1724. */
  1725. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1726. pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  1727. status = qdf_nbuf_map(target->osdev,
  1728. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  1729. QDF_DMA_TO_DEVICE);
  1730. if (status == QDF_STATUS_SUCCESS) {
  1731. target->nbuf_nfc_map_count++;
  1732. } else {
  1733. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1734. ("%s: nbuf map failed, endpoint %pK, seq_no. %d\n",
  1735. __func__, pEndpoint, pEndpoint->SeqNo));
  1736. return status;
  1737. }
  1738. }
  1739. INIT_HTC_PACKET_QUEUE_AND_ADD(&pPktQueue, pPacket);
  1740. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1741. if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id))
  1742. htc_send_pkts_sched_queue(HTCHandle, &pPktQueue, pEndpoint->Id);
  1743. else
  1744. htc_try_send(target, pEndpoint, &pPktQueue);
  1745. #else
  1746. htc_try_send(target, pEndpoint, &pPktQueue);
  1747. #endif
  1748. /* do completion on any packets that couldn't get in */
  1749. while (!HTC_QUEUE_EMPTY(&pPktQueue)) {
  1750. pPacket = htc_packet_dequeue(&pPktQueue);
  1751. if (HTC_STOPPING(target))
  1752. pPacket->Status = QDF_STATUS_E_CANCELED;
  1753. else
  1754. pPacket->Status = QDF_STATUS_E_RESOURCES;
  1755. send_packet_completion(target, pPacket);
  1756. }
  1757. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
  1758. return QDF_STATUS_SUCCESS;
  1759. }
  1760. #ifdef CUSTOM_CB_SCHEDULER_SUPPORT
  1761. /**
  1762. * htc_get_endpoint_ul_pipeid() - Helper API to get uplink pipe ID
  1763. * @htc_handle: HTC handle
  1764. * @endpoint_id: Endpoint ID
  1765. * @ul_pipeid: Pointer to uplink pipe ID
  1766. *
  1767. * return: QDF_STATUS
  1768. */
  1769. static QDF_STATUS
  1770. htc_get_endpoint_ul_pipeid(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id,
  1771. uint8_t *ul_pipeid)
  1772. {
  1773. HTC_TARGET *target;
  1774. HTC_ENDPOINT *end_point;
  1775. if (!htc_handle) {
  1776. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1777. ("%s: HTCHandle is NULL\n", __func__));
  1778. return QDF_STATUS_E_NULL_VALUE;
  1779. }
  1780. target = GET_HTC_TARGET_FROM_HANDLE(htc_handle);
  1781. if (endpoint_id >= ENDPOINT_MAX || endpoint_id <= ENDPOINT_UNUSED) {
  1782. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1783. ("%s endpoint is invalid\n", __func__));
  1784. AR_DEBUG_ASSERT(0);
  1785. return QDF_STATUS_E_INVAL;
  1786. }
  1787. end_point = &target->endpoint[endpoint_id];
  1788. if (!end_point->service_id) {
  1789. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
  1790. __func__));
  1791. return QDF_STATUS_E_INVAL;
  1792. }
  1793. *ul_pipeid = end_point->UL_PipeID;
  1794. return QDF_STATUS_SUCCESS;
  1795. }
  1796. QDF_STATUS
  1797. htc_register_custom_cb(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id,
  1798. void (*custom_cb)(void *), void *custom_cb_context)
  1799. {
  1800. QDF_STATUS status = QDF_STATUS_SUCCESS;
  1801. uint8_t ul_pipeid;
  1802. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+%s\n", __func__));
  1803. status = htc_get_endpoint_ul_pipeid(htc_handle, endpoint_id,
  1804. &ul_pipeid);
  1805. if (QDF_IS_STATUS_ERROR(status)) {
  1806. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to get ul pipeid\n",
  1807. __func__));
  1808. goto exit;
  1809. }
  1810. status = hif_register_ce_custom_cb(htc_get_hif_device(htc_handle),
  1811. ul_pipeid, custom_cb,
  1812. custom_cb_context);
  1813. if (QDF_IS_STATUS_ERROR(status)) {
  1814. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to register cb\n",
  1815. __func__));
  1816. goto exit;
  1817. }
  1818. exit:
  1819. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-%s\n", __func__));
  1820. return status;
  1821. }
  1822. QDF_STATUS
  1823. htc_unregister_custom_cb(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id)
  1824. {
  1825. QDF_STATUS status = QDF_STATUS_SUCCESS;
  1826. uint8_t ul_pipeid;
  1827. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+%s\n", __func__));
  1828. status = htc_get_endpoint_ul_pipeid(htc_handle, endpoint_id,
  1829. &ul_pipeid);
  1830. if (QDF_IS_STATUS_ERROR(status)) {
  1831. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to get ul pipeid\n",
  1832. __func__));
  1833. goto exit;
  1834. }
  1835. status = hif_unregister_ce_custom_cb(htc_get_hif_device(htc_handle),
  1836. ul_pipeid);
  1837. if (QDF_IS_STATUS_ERROR(status)) {
  1838. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to unregister cb\n",
  1839. __func__));
  1840. status = QDF_STATUS_E_INVAL;
  1841. goto exit;
  1842. }
  1843. exit:
  1844. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-%s\n", __func__));
  1845. return status;
  1846. }
  1847. QDF_STATUS
  1848. htc_enable_custom_cb(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id)
  1849. {
  1850. QDF_STATUS status = QDF_STATUS_SUCCESS;
  1851. uint8_t ul_pipeid;
  1852. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+%s\n", __func__));
  1853. status = htc_get_endpoint_ul_pipeid(htc_handle, endpoint_id,
  1854. &ul_pipeid);
  1855. if (QDF_IS_STATUS_ERROR(status)) {
  1856. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to get ul pipeid\n",
  1857. __func__));
  1858. goto exit;
  1859. }
  1860. status = hif_enable_ce_custom_cb(htc_get_hif_device(htc_handle),
  1861. ul_pipeid);
  1862. if (QDF_IS_STATUS_ERROR(status)) {
  1863. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to enable cb\n",
  1864. __func__));
  1865. status = QDF_STATUS_E_INVAL;
  1866. goto exit;
  1867. }
  1868. exit:
  1869. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-%s\n", __func__));
  1870. return status;
  1871. }
  1872. QDF_STATUS
  1873. htc_disable_custom_cb(HTC_HANDLE htc_handle, HTC_ENDPOINT_ID endpoint_id)
  1874. {
  1875. QDF_STATUS status = QDF_STATUS_SUCCESS;
  1876. uint8_t ul_pipeid;
  1877. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+%s\n", __func__));
  1878. status = htc_get_endpoint_ul_pipeid(htc_handle, endpoint_id,
  1879. &ul_pipeid);
  1880. if (QDF_IS_STATUS_ERROR(status)) {
  1881. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to get ul pipeid\n",
  1882. __func__));
  1883. goto exit;
  1884. }
  1885. status = hif_disable_ce_custom_cb(htc_get_hif_device(htc_handle),
  1886. ul_pipeid);
  1887. if (QDF_IS_STATUS_ERROR(status)) {
  1888. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s Failed to disable cb\n",
  1889. __func__));
  1890. status = QDF_STATUS_E_INVAL;
  1891. goto exit;
  1892. }
  1893. exit:
  1894. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-%s\n", __func__));
  1895. return status;
  1896. }
  1897. #endif /* CUSTOM_CB_SCHEDULER_SUPPORT */
  1898. /* HTC API - htc_send_pkt */
  1899. QDF_STATUS htc_send_pkt(HTC_HANDLE htc_handle, HTC_PACKET *htc_packet)
  1900. {
  1901. if (!htc_handle) {
  1902. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1903. ("%s: HTCHandle is NULL \n", __func__));
  1904. return QDF_STATUS_E_FAILURE;
  1905. }
  1906. if (!htc_packet) {
  1907. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1908. ("%s: pPacket is NULL \n", __func__));
  1909. return QDF_STATUS_E_FAILURE;
  1910. }
  1911. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1912. ("+-htc_send_pkt: Enter endPointId: %d, buffer: %pK, length: %d\n",
  1913. htc_packet->Endpoint, htc_packet->pBuffer,
  1914. htc_packet->ActualLength));
  1915. return __htc_send_pkt(htc_handle, htc_packet);
  1916. }
  1917. qdf_export_symbol(htc_send_pkt);
  1918. #ifdef ATH_11AC_TXCOMPACT
  1919. /**
  1920. * htc_send_data_pkt() - send single data packet on an endpoint
  1921. * @htc_hdl: pointer to HTC handle
  1922. * @netbuf: network buffer containing the data to be sent
  1923. * @ep_id: endpoint identifier
  1924. * @actual_length: length of data that needs to be transmitted
  1925. *
  1926. * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
  1927. */
  1928. QDF_STATUS htc_send_data_pkt(HTC_HANDLE htc_hdl, qdf_nbuf_t netbuf, int ep_id,
  1929. int actual_length)
  1930. {
  1931. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
  1932. HTC_ENDPOINT *pEndpoint;
  1933. HTC_FRAME_HDR *p_htc_hdr;
  1934. QDF_STATUS status = QDF_STATUS_SUCCESS;
  1935. int tx_resources;
  1936. uint32_t data_attr = 0;
  1937. int htc_payload_len = actual_length;
  1938. unsigned int rtpm_code;
  1939. pEndpoint = &target->endpoint[ep_id];
  1940. tx_resources = hif_get_free_queue_number(target->hif_dev,
  1941. pEndpoint->UL_PipeID);
  1942. if (tx_resources < HTC_DATA_RESOURCE_THRS) {
  1943. if (pEndpoint->ul_is_polled) {
  1944. hif_send_complete_check(pEndpoint->target->hif_dev,
  1945. pEndpoint->UL_PipeID, 1);
  1946. tx_resources =
  1947. hif_get_free_queue_number(target->hif_dev,
  1948. pEndpoint->UL_PipeID);
  1949. }
  1950. if (tx_resources < HTC_DATA_MINDESC_PERPACKET)
  1951. return QDF_STATUS_E_FAILURE;
  1952. }
  1953. rtpm_code = htc_send_pkts_get_rtpm_id(
  1954. pEndpoint->service_id);
  1955. if (hif_rtpm_get(HIF_RTPM_GET_ASYNC, rtpm_code))
  1956. return QDF_STATUS_E_FAILURE;
  1957. p_htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1958. AR_DEBUG_ASSERT(p_htc_hdr);
  1959. data_attr = qdf_nbuf_data_attr_get(netbuf);
  1960. if (target->htc_hdr_length_check)
  1961. htc_payload_len = actual_length - HTC_HEADER_LEN;
  1962. HTC_WRITE32(p_htc_hdr, SM(htc_payload_len, HTC_FRAME_HDR_PAYLOADLEN)
  1963. | SM(ep_id, HTC_FRAME_HDR_ENDPOINTID));
  1964. /*
  1965. * If the HIF pipe for the data endpoint is polled rather than
  1966. * interrupt-driven, this is a good point to check whether any
  1967. * data previously sent through the HIF pipe have finished being
  1968. * sent.
  1969. * Since this may result in callbacks to htc_tx_completion_handler,
  1970. * which can take the HTC tx lock, make the hif_send_complete_check
  1971. * call before acquiring the HTC tx lock.
  1972. * Call hif_send_complete_check directly, rather than calling
  1973. * htc_send_complete_check, and call the PollTimerStart separately
  1974. * after calling hif_send_head, so the timer will be started to
  1975. * check for completion of the new outstanding download (in the
  1976. * unexpected event that other polling calls don't catch it).
  1977. */
  1978. LOCK_HTC_TX(target);
  1979. HTC_WRITE32(((uint32_t *)p_htc_hdr) + 1,
  1980. SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
  1981. pEndpoint->SeqNo++;
  1982. QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
  1983. DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1984. QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
  1985. sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
  1986. status = hif_send_head(target->hif_dev,
  1987. pEndpoint->UL_PipeID,
  1988. pEndpoint->Id, actual_length, netbuf, data_attr);
  1989. UNLOCK_HTC_TX(target);
  1990. return status;
  1991. }
  1992. #else /*ATH_11AC_TXCOMPACT */
  1993. /**
  1994. * htc_send_data_pkt() - htc_send_data_pkt
  1995. * @HTCHandle: pointer to HTC handle
  1996. * @pPacket: pointer to HTC_PACKET
  1997. * @more_data: indicates whether more data is to follow
  1998. *
  1999. * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
  2000. */
  2001. QDF_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
  2002. uint8_t more_data)
  2003. {
  2004. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  2005. HTC_ENDPOINT *pEndpoint;
  2006. HTC_FRAME_HDR *pHtcHdr;
  2007. HTC_PACKET_QUEUE sendQueue;
  2008. qdf_nbuf_t netbuf = NULL;
  2009. int tx_resources;
  2010. QDF_STATUS status = QDF_STATUS_SUCCESS;
  2011. uint32_t data_attr = 0;
  2012. bool used_extra_tx_credit = false;
  2013. if (pPacket) {
  2014. if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
  2015. (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
  2016. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  2017. ("%s endpoint is invalid\n", __func__));
  2018. AR_DEBUG_ASSERT(0);
  2019. return QDF_STATUS_E_INVAL;
  2020. }
  2021. pEndpoint = &target->endpoint[pPacket->Endpoint];
  2022. /* add HTC_FRAME_HDR in the initial fragment */
  2023. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  2024. pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
  2025. AR_DEBUG_ASSERT(pHtcHdr);
  2026. HTC_WRITE32(pHtcHdr,
  2027. SM(pPacket->ActualLength,
  2028. HTC_FRAME_HDR_PAYLOADLEN) |
  2029. SM(pPacket->PktInfo.AsTx.SendFlags,
  2030. HTC_FRAME_HDR_FLAGS) |
  2031. SM(pPacket->Endpoint,
  2032. HTC_FRAME_HDR_ENDPOINTID));
  2033. /*
  2034. * If the HIF pipe for the data endpoint is polled rather than
  2035. * interrupt-driven, this is a good point to check whether any
  2036. * data previously sent through the HIF pipe have finished being
  2037. * sent. Since this may result in callbacks to
  2038. * htc_tx_completion_handler, which can take the HTC tx lock,
  2039. * make the hif_send_complete_check call before acquiring the
  2040. * HTC tx lock.
  2041. * Call hif_send_complete_check directly, rather than calling
  2042. * htc_send_complete_check, and call the PollTimerStart
  2043. * separately after calling hif_send_head, so the timer will be
  2044. * started to check for completion of the new outstanding
  2045. * download (in the unexpected event that other polling calls
  2046. * don't catch it).
  2047. */
  2048. if (pEndpoint->ul_is_polled) {
  2049. htc_send_complete_poll_timer_stop(pEndpoint);
  2050. hif_send_complete_check(pEndpoint->target->hif_dev,
  2051. pEndpoint->UL_PipeID, 0);
  2052. }
  2053. LOCK_HTC_TX(target);
  2054. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  2055. pEndpoint->SeqNo++;
  2056. HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
  2057. SM(pPacket->PktInfo.AsTx.SeqNo,
  2058. HTC_FRAME_HDR_CONTROLBYTES1));
  2059. /* append new packet to pEndpoint->TxQueue */
  2060. HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
  2061. if (HTC_TX_BUNDLE_ENABLED(target) && (more_data)) {
  2062. UNLOCK_HTC_TX(target);
  2063. return QDF_STATUS_SUCCESS;
  2064. }
  2065. QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
  2066. DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  2067. QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
  2068. sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
  2069. } else {
  2070. LOCK_HTC_TX(target);
  2071. pEndpoint = &target->endpoint[1];
  2072. }
  2073. /* increment tx processing count on entry */
  2074. qdf_atomic_inc(&pEndpoint->TxProcessCount);
  2075. if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  2076. /*
  2077. * Another thread or task is draining the TX queues on this
  2078. * endpoint. That thread will reset the tx processing count when
  2079. * the queue is drained.
  2080. */
  2081. qdf_atomic_dec(&pEndpoint->TxProcessCount);
  2082. UNLOCK_HTC_TX(target);
  2083. return QDF_STATUS_SUCCESS;
  2084. }
  2085. /***** beyond this point only 1 thread may enter ******/
  2086. INIT_HTC_PACKET_QUEUE(&sendQueue);
  2087. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  2088. #if DEBUG_CREDIT
  2089. int cred = pEndpoint->TxCredits;
  2090. #endif
  2091. get_htc_send_packets_credit_based(target, pEndpoint,
  2092. &sendQueue);
  2093. #if DEBUG_CREDIT
  2094. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  2095. if (cred - pEndpoint->TxCredits > 0) {
  2096. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  2097. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  2098. pEndpoint->Id, cred,
  2099. cred - pEndpoint->TxCredits,
  2100. pEndpoint->TxCredits));
  2101. }
  2102. }
  2103. #endif
  2104. UNLOCK_HTC_TX(target);
  2105. }
  2106. else if (HTC_TX_BUNDLE_ENABLED(target)) {
  2107. if (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB) {
  2108. if (hif_get_free_queue_number(target->hif_dev,
  2109. pEndpoint->UL_PipeID))
  2110. /*
  2111. * Header and payload belongs to the different
  2112. * fragments and consume 2 resource for one HTC
  2113. * package but USB combine into one transfer.
  2114. */
  2115. get_htc_send_packets(target, pEndpoint,
  2116. &sendQueue,
  2117. HTC_MAX_MSG_PER_BUNDLE_TX
  2118. * 2);
  2119. } else {
  2120. /* Dequeue max packets from endpoint tx queue */
  2121. get_htc_send_packets(target, pEndpoint, &sendQueue,
  2122. HTC_MAX_TX_BUNDLE_SEND_LIMIT);
  2123. }
  2124. UNLOCK_HTC_TX(target);
  2125. } else {
  2126. /*
  2127. * Now drain the endpoint TX queue for transmission as long as
  2128. * we have enough transmit resources
  2129. */
  2130. tx_resources =
  2131. hif_get_free_queue_number(target->hif_dev,
  2132. pEndpoint->UL_PipeID);
  2133. get_htc_send_packets(target, pEndpoint, &sendQueue,
  2134. tx_resources);
  2135. UNLOCK_HTC_TX(target);
  2136. }
  2137. /* send what we can */
  2138. while (true) {
  2139. if (HTC_TX_BUNDLE_ENABLED(target) &&
  2140. (HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
  2141. HTC_MIN_MSG_PER_BUNDLE) &&
  2142. (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO ||
  2143. hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)) {
  2144. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  2145. if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
  2146. status = QDF_STATUS_E_RESOURCES;
  2147. /* put the sendQueue back at the front
  2148. * of pEndpoint->TxQueue
  2149. */
  2150. LOCK_HTC_TX(target);
  2151. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(
  2152. &pEndpoint->TxQueue,
  2153. &sendQueue);
  2154. UNLOCK_HTC_TX(target);
  2155. break;
  2156. }
  2157. }
  2158. htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
  2159. }
  2160. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  2161. if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
  2162. status = QDF_STATUS_E_RESOURCES;
  2163. /* put the sendQueue back at the front
  2164. * of pEndpoint->TxQueue
  2165. */
  2166. LOCK_HTC_TX(target);
  2167. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(
  2168. &pEndpoint->TxQueue,
  2169. &sendQueue);
  2170. UNLOCK_HTC_TX(target);
  2171. break;
  2172. }
  2173. }
  2174. pPacket = htc_packet_dequeue(&sendQueue);
  2175. if (!pPacket)
  2176. break;
  2177. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  2178. pHtcHdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  2179. LOCK_HTC_TX(target);
  2180. /* store in look up queue to match completions */
  2181. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  2182. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  2183. pEndpoint->ul_outstanding_cnt++;
  2184. UNLOCK_HTC_TX(target);
  2185. used_extra_tx_credit =
  2186. htc_handle_extra_tx_credit(pEndpoint, pPacket,
  2187. (uint8_t *)pHtcHdr,
  2188. NULL,
  2189. pPacket->ActualLength
  2190. + HTC_HDR_LENGTH);
  2191. status = hif_send_head(target->hif_dev,
  2192. pEndpoint->UL_PipeID,
  2193. pEndpoint->Id,
  2194. HTC_HDR_LENGTH + pPacket->ActualLength,
  2195. netbuf, data_attr);
  2196. if (status != QDF_STATUS_SUCCESS) {
  2197. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  2198. if (used_extra_tx_credit) {
  2199. pEndpoint->EpCallBacks.
  2200. ep_padding_credit_update
  2201. (pEndpoint->EpCallBacks.pContext, 1);
  2202. }
  2203. }
  2204. }
  2205. #if DEBUG_BUNDLE
  2206. qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
  2207. pEndpoint->Id,
  2208. pEndpoint->TxCreditSize,
  2209. HTC_HDR_LENGTH + pPacket->ActualLength);
  2210. #endif
  2211. htc_issue_tx_bundle_stats_inc(target);
  2212. if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
  2213. LOCK_HTC_TX(target);
  2214. pEndpoint->ul_outstanding_cnt--;
  2215. /* remove this packet from the tx completion queue */
  2216. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  2217. /*
  2218. * Don't bother reclaiming credits - HTC flow control
  2219. * is not applicable to tx data.
  2220. * In LL systems, there is no download flow control,
  2221. * since there's virtually no download delay.
  2222. * In HL systems, the txrx SW explicitly performs the
  2223. * tx flow control.
  2224. */
  2225. /* pEndpoint->TxCredits +=
  2226. * pPacket->PktInfo.AsTx.CreditsUsed;
  2227. */
  2228. /* put this frame back at the front of the sendQueue */
  2229. HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
  2230. /* put the sendQueue back at the front of
  2231. * pEndpoint->TxQueue
  2232. */
  2233. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  2234. &sendQueue);
  2235. UNLOCK_HTC_TX(target);
  2236. break; /* still need to reset TxProcessCount */
  2237. }
  2238. }
  2239. /* done with this endpoint, we can clear the count */
  2240. qdf_atomic_init(&pEndpoint->TxProcessCount);
  2241. if (pEndpoint->ul_is_polled) {
  2242. /*
  2243. * Start a cleanup timer to poll for download completion.
  2244. * The download completion should be noticed promptly from
  2245. * other polling calls, but the timer provides a safety net
  2246. * in case other polling calls don't occur as expected.
  2247. */
  2248. htc_send_complete_poll_timer_start(pEndpoint);
  2249. }
  2250. return status;
  2251. }
  2252. #endif /*ATH_11AC_TXCOMPACT */
  2253. qdf_export_symbol(htc_send_data_pkt);
  2254. /*
  2255. * In the adapted HIF layer, qdf_nbuf_t are passed between HIF and HTC,
  2256. * since upper layers expects HTC_PACKET containers we use the completed netbuf
  2257. * and lookup its corresponding HTC packet buffer from a lookup list.
  2258. * This is extra overhead that can be fixed by re-aligning HIF interfaces
  2259. * with HTC.
  2260. *
  2261. */
  2262. static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
  2263. HTC_ENDPOINT *pEndpoint,
  2264. qdf_nbuf_t netbuf)
  2265. {
  2266. HTC_PACKET *pPacket = NULL;
  2267. HTC_PACKET *pFoundPacket = NULL;
  2268. HTC_PACKET_QUEUE lookupQueue;
  2269. INIT_HTC_PACKET_QUEUE(&lookupQueue);
  2270. LOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  2271. LOCK_HTC_TX(target);
  2272. /* mark that HIF has indicated the send complete for another packet */
  2273. pEndpoint->ul_outstanding_cnt--;
  2274. /* Dequeue first packet directly because of in-order completion */
  2275. pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
  2276. if (qdf_unlikely(!pPacket)) {
  2277. UNLOCK_HTC_TX(target);
  2278. UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  2279. return NULL;
  2280. }
  2281. if (netbuf == (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  2282. UNLOCK_HTC_TX(target);
  2283. UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  2284. return pPacket;
  2285. }
  2286. HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
  2287. /*
  2288. * Move TX lookup queue to temp queue because most of packets that are
  2289. * not index 0 are not top 10 packets.
  2290. */
  2291. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
  2292. &pEndpoint->TxLookupQueue);
  2293. ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
  2294. HTC_PACKET, ListLink) {
  2295. if (!pPacket) {
  2296. pFoundPacket = pPacket;
  2297. break;
  2298. }
  2299. /* check for removal */
  2300. if (netbuf ==
  2301. (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  2302. /* found it */
  2303. HTC_PACKET_REMOVE(&lookupQueue, pPacket);
  2304. pFoundPacket = pPacket;
  2305. break;
  2306. }
  2307. }
  2308. ITERATE_END;
  2309. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
  2310. &lookupQueue);
  2311. UNLOCK_HTC_TX(target);
  2312. UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  2313. return pFoundPacket;
  2314. }
  2315. /**
  2316. * htc_tx_completion_handler() - htc tx completion handler
  2317. * @Context: pointer to HTC_TARGET structure
  2318. * @netbuf: pointer to netbuf for which completion handler is being called
  2319. * @EpID: end point Id on which the packet was sent
  2320. * @toeplitz_hash_result: toeplitz hash result
  2321. *
  2322. * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
  2323. */
  2324. QDF_STATUS htc_tx_completion_handler(void *Context,
  2325. qdf_nbuf_t netbuf, unsigned int EpID,
  2326. uint32_t toeplitz_hash_result)
  2327. {
  2328. HTC_TARGET *target = (HTC_TARGET *) Context;
  2329. HTC_ENDPOINT *pEndpoint;
  2330. HTC_PACKET *pPacket;
  2331. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  2332. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5, ENDPOINT_4,
  2333. ENDPOINT_2, ENDPOINT_3 };
  2334. int epidIdx;
  2335. uint16_t resourcesThresh[DATA_EP_SIZE]; /* urb resources */
  2336. uint16_t resources;
  2337. uint16_t resourcesMax;
  2338. #endif
  2339. pEndpoint = &target->endpoint[EpID];
  2340. target->TX_comp_cnt++;
  2341. pEndpoint->htc_comp_cnt++;
  2342. do {
  2343. pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
  2344. if (!pPacket) {
  2345. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  2346. ("HTC TX lookup failed!\n"));
  2347. /* may have already been flushed and freed */
  2348. netbuf = NULL;
  2349. break;
  2350. }
  2351. if (pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_AUTO_PM &&
  2352. pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_RUNTIME_PUT &&
  2353. pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_RTPM_PUT_RC) {
  2354. hif_rtpm_put(HIF_RTPM_PUT_ASYNC, HIF_RTPM_ID_WMI);
  2355. htc_dec_wmi_runtime_cnt(target, HIF_RTPM_ID_WMI);
  2356. }
  2357. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
  2358. HTC_PACKET *pPacketTemp;
  2359. HTC_PACKET_QUEUE *pQueueSave =
  2360. (HTC_PACKET_QUEUE *) pPacket->pContext;
  2361. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
  2362. pPacketTemp) {
  2363. pPacket->Status = QDF_STATUS_SUCCESS;
  2364. send_packet_completion(target, pPacketTemp);
  2365. }
  2366. HTC_PACKET_QUEUE_ITERATE_END;
  2367. free_htc_bundle_packet(target, pPacket);
  2368. if (hif_get_bus_type(target->hif_dev) ==
  2369. QDF_BUS_TYPE_USB) {
  2370. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
  2371. htc_try_send(target, pEndpoint, NULL);
  2372. }
  2373. return QDF_STATUS_SUCCESS;
  2374. }
  2375. /* will be giving this buffer back to upper layers */
  2376. netbuf = NULL;
  2377. pPacket->Status = QDF_STATUS_SUCCESS;
  2378. send_packet_completion(target, pPacket);
  2379. } while (false);
  2380. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  2381. /* note: when using TX credit flow, the re-checking of queues
  2382. * happens when credits flow back from the target. In the non-TX
  2383. * credit case, we recheck after the packet completes
  2384. */
  2385. if ((qdf_atomic_read(&pEndpoint->TxProcessCount) == 0) ||
  2386. (!pEndpoint->async_update)) {
  2387. htc_try_send(target, pEndpoint, NULL);
  2388. }
  2389. }
  2390. return QDF_STATUS_SUCCESS;
  2391. }
  2392. #ifdef WLAN_FEATURE_FASTPATH
  2393. /**
  2394. * htc_ctrl_msg_cmpl() - checks for tx completion for the endpoint specified
  2395. * @htc_pdev: pointer to the htc context
  2396. * @htc_ep_id: end point id
  2397. *
  2398. * checks HTC tx completion
  2399. *
  2400. * Return: none
  2401. */
  2402. void htc_ctrl_msg_cmpl(HTC_HANDLE htc_pdev, HTC_ENDPOINT_ID htc_ep_id)
  2403. {
  2404. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_pdev);
  2405. HTC_ENDPOINT *pendpoint = &target->endpoint[htc_ep_id];
  2406. htc_send_complete_check(pendpoint, 1);
  2407. }
  2408. qdf_export_symbol(htc_ctrl_msg_cmpl);
  2409. #endif
  2410. /* callback when TX resources become available */
  2411. void htc_tx_resource_avail_handler(void *context, uint8_t pipeID)
  2412. {
  2413. int i;
  2414. HTC_TARGET *target = (HTC_TARGET *) context;
  2415. HTC_ENDPOINT *pEndpoint = NULL;
  2416. for (i = 0; i < ENDPOINT_MAX; i++) {
  2417. pEndpoint = &target->endpoint[i];
  2418. if (pEndpoint->service_id != 0) {
  2419. if (pEndpoint->UL_PipeID == pipeID)
  2420. break;
  2421. }
  2422. }
  2423. if (i >= ENDPOINT_MAX) {
  2424. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  2425. ("Invalid pipe indicated for TX resource avail : %d!\n",
  2426. pipeID));
  2427. return;
  2428. }
  2429. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  2430. ("HIF indicated more resources for pipe:%d\n",
  2431. pipeID));
  2432. htc_try_send(target, pEndpoint, NULL);
  2433. }
  2434. #ifdef FEATURE_RUNTIME_PM
  2435. /**
  2436. * htc_kick_queues() - resumes tx transactions of suspended endpoints
  2437. * @context: pointer to the htc target context
  2438. *
  2439. * Iterates through the endpoints and provides a context to empty queues
  2440. * int the hif layer when they are stalled due to runtime suspend.
  2441. *
  2442. * Return: none
  2443. */
  2444. void htc_kick_queues(void *context)
  2445. {
  2446. int i;
  2447. HTC_TARGET *target = (HTC_TARGET *)context;
  2448. HTC_ENDPOINT *endpoint = NULL;
  2449. if (hif_rtpm_get(HIF_RTPM_GET_SYNC, HIF_RTPM_ID_HTT))
  2450. return;
  2451. for (i = 0; i < ENDPOINT_MAX; i++) {
  2452. endpoint = &target->endpoint[i];
  2453. if (endpoint->service_id == 0)
  2454. continue;
  2455. if (endpoint->EpCallBacks.ep_resume_tx_queue)
  2456. endpoint->EpCallBacks.ep_resume_tx_queue(
  2457. endpoint->EpCallBacks.pContext);
  2458. htc_try_send(target, endpoint, NULL);
  2459. }
  2460. hif_fastpath_resume(target->hif_dev);
  2461. hif_rtpm_put(HIF_RTPM_PUT_ASYNC, HIF_RTPM_ID_HTT);
  2462. }
  2463. #endif
  2464. /* flush endpoint TX queue */
  2465. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  2466. HTC_TX_TAG Tag)
  2467. {
  2468. HTC_PACKET *pPacket;
  2469. LOCK_HTC_TX(target);
  2470. while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  2471. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  2472. if (pPacket) {
  2473. /* let the sender know the packet was not delivered */
  2474. pPacket->Status = QDF_STATUS_E_CANCELED;
  2475. send_packet_completion(target, pPacket);
  2476. }
  2477. }
  2478. UNLOCK_HTC_TX(target);
  2479. }
  2480. /* flush pending entries in endpoint TX Lookup queue */
  2481. void htc_flush_endpoint_txlookupQ(HTC_TARGET *target,
  2482. HTC_ENDPOINT_ID endpoint_id,
  2483. bool call_ep_callback)
  2484. {
  2485. HTC_PACKET *packet;
  2486. HTC_ENDPOINT *endpoint;
  2487. endpoint = &target->endpoint[endpoint_id];
  2488. if (!endpoint && endpoint->service_id == 0)
  2489. return;
  2490. LOCK_HTC_TX(target);
  2491. while (HTC_PACKET_QUEUE_DEPTH(&endpoint->TxLookupQueue)) {
  2492. packet = htc_packet_dequeue(&endpoint->TxLookupQueue);
  2493. if (packet) {
  2494. if (call_ep_callback == true) {
  2495. packet->Status = QDF_STATUS_E_CANCELED;
  2496. send_packet_completion(target, packet);
  2497. } else {
  2498. qdf_mem_free(packet);
  2499. }
  2500. }
  2501. }
  2502. UNLOCK_HTC_TX(target);
  2503. }
  2504. /* HTC API to flush an endpoint's TX queue*/
  2505. void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
  2506. HTC_TX_TAG Tag)
  2507. {
  2508. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  2509. HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
  2510. if (pEndpoint->service_id == 0) {
  2511. AR_DEBUG_ASSERT(false);
  2512. /* not in use.. */
  2513. return;
  2514. }
  2515. htc_flush_endpoint_tx(target, pEndpoint, Tag);
  2516. }
  2517. /* HTC API to indicate activity to the credit distribution function */
  2518. void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
  2519. HTC_ENDPOINT_ID Endpoint, bool Active)
  2520. {
  2521. /* TODO */
  2522. }
  2523. bool htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
  2524. {
  2525. return true;
  2526. }
  2527. void htc_set_pkt_dbg(HTC_HANDLE handle, A_BOOL dbg_flag)
  2528. {
  2529. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(handle);
  2530. target->htc_pkt_dbg = dbg_flag;
  2531. }
  2532. void htc_set_nodrop_pkt(HTC_HANDLE HTCHandle, A_BOOL isNodropPkt)
  2533. {
  2534. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  2535. target->is_nodrop_pkt = isNodropPkt;
  2536. }
  2537. void htc_enable_hdr_length_check(HTC_HANDLE htc_hdl, bool htc_hdr_length_check)
  2538. {
  2539. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
  2540. target->htc_hdr_length_check = htc_hdr_length_check;
  2541. }
  2542. /**
  2543. * htc_process_credit_rpt() - process credit report, call distribution function
  2544. * @target: pointer to HTC_TARGET
  2545. * @pRpt: pointer to HTC_CREDIT_REPORT
  2546. * @NumEntries: number of entries in credit report
  2547. * @FromEndpoint: endpoint for which credit report is received
  2548. *
  2549. * Return: A_OK for success or an appropriate A_STATUS error
  2550. */
  2551. void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
  2552. int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
  2553. {
  2554. int i;
  2555. HTC_ENDPOINT *pEndpoint;
  2556. int totalCredits = 0;
  2557. uint8_t rpt_credits, rpt_ep_id;
  2558. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  2559. ("+htc_process_credit_rpt, Credit Report Entries:%d\n",
  2560. NumEntries));
  2561. /* lock out TX while we update credits */
  2562. LOCK_HTC_TX(target);
  2563. for (i = 0; i < NumEntries; i++, pRpt++) {
  2564. rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
  2565. if (rpt_ep_id >= ENDPOINT_MAX) {
  2566. AR_DEBUG_ASSERT(false);
  2567. break;
  2568. }
  2569. rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
  2570. pEndpoint = &target->endpoint[rpt_ep_id];
  2571. #if DEBUG_CREDIT
  2572. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  2573. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  2574. (" <HTC> Increase EP%d %d + %d = %d credits\n",
  2575. rpt_ep_id, pEndpoint->TxCredits,
  2576. rpt_credits,
  2577. pEndpoint->TxCredits + rpt_credits));
  2578. }
  2579. #endif
  2580. #ifdef HTC_EP_STAT_PROFILING
  2581. INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
  2582. INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
  2583. if (FromEndpoint == rpt_ep_id) {
  2584. /* this credit report arrived on the same endpoint
  2585. * indicating it arrived in an RX packet
  2586. */
  2587. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
  2588. rpt_credits);
  2589. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
  2590. } else if (FromEndpoint == ENDPOINT_0) {
  2591. /* this credit arrived on endpoint 0 as a NULL msg */
  2592. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
  2593. rpt_credits);
  2594. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
  2595. } else {
  2596. /* arrived on another endpoint */
  2597. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
  2598. rpt_credits);
  2599. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
  2600. }
  2601. #endif
  2602. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  2603. htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
  2604. pEndpoint->TxCredits + rpt_credits,
  2605. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  2606. TxQueue));
  2607. hif_latency_detect_credit_record_time(
  2608. HIF_PROCESS_CREDIT_REPORT,
  2609. target->hif_dev);
  2610. }
  2611. pEndpoint->TxCredits += rpt_credits;
  2612. if (pEndpoint->TxCredits
  2613. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  2614. UNLOCK_HTC_TX(target);
  2615. #ifdef ATH_11AC_TXCOMPACT
  2616. htc_try_send(target, pEndpoint, NULL);
  2617. #else
  2618. if (pEndpoint->service_id == HTT_DATA_MSG_SVC)
  2619. htc_send_data_pkt((HTC_HANDLE)target, NULL, 0);
  2620. else
  2621. htc_try_send(target, pEndpoint, NULL);
  2622. #endif
  2623. LOCK_HTC_TX(target);
  2624. }
  2625. totalCredits += rpt_credits;
  2626. }
  2627. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  2628. (" Report indicated %d credits to distribute\n",
  2629. totalCredits));
  2630. UNLOCK_HTC_TX(target);
  2631. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt\n"));
  2632. }
  2633. /* function to fetch stats from htc layer*/
  2634. struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
  2635. {
  2636. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  2637. return &(target->htc_pkt_stats);
  2638. }
  2639. #ifdef SYSTEM_PM_CHECK
  2640. void htc_system_resume(HTC_HANDLE htc)
  2641. {
  2642. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc);
  2643. HTC_ENDPOINT *endpoint = NULL;
  2644. int i;
  2645. if (!target)
  2646. return;
  2647. for (i = 0; i < ENDPOINT_MAX; i++) {
  2648. endpoint = &target->endpoint[i];
  2649. if (endpoint->service_id == 0)
  2650. continue;
  2651. htc_try_send(target, endpoint, NULL);
  2652. }
  2653. }
  2654. #endif