htc_send.c 71 KB

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