htc_send.c 83 KB

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