htc_send.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560
  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. HTC_FLAGS_SEND_BUNDLE,
  534. HTC_FRAME_HDR_FLAGS) |
  535. SM(pPacket->Endpoint,
  536. HTC_FRAME_HDR_ENDPOINTID));
  537. HTC_WRITE32((uint32_t *) pHtcHdr + 1,
  538. SM(pPacket->PktInfo.AsTx.SeqNo,
  539. HTC_FRAME_HDR_CONTROLBYTES1) | SM(creditPad,
  540. HTC_FRAME_HDR_RESERVED));
  541. pHtcHdr->reserved = creditPad;
  542. }
  543. frag_count = qdf_nbuf_get_num_frags(netbuf);
  544. nbytes = pPacket->ActualLength + HTC_HDR_LENGTH;
  545. for (i = 0; i < frag_count && nbytes > 0; i++) {
  546. int frag_len = qdf_nbuf_get_frag_len(netbuf, i);
  547. unsigned char *frag_addr =
  548. qdf_nbuf_get_frag_vaddr(netbuf, i);
  549. if (frag_len > nbytes)
  550. frag_len = nbytes;
  551. qdf_mem_copy(pBundleBuffer, frag_addr, frag_len);
  552. nbytes -= frag_len;
  553. pBundleBuffer += frag_len;
  554. }
  555. HTC_PACKET_ENQUEUE(pQueueSave, pPacket);
  556. pBundleBuffer += creditPad;
  557. /* last one can't be packed. */
  558. if (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)
  559. last_credit_pad = creditPad;
  560. }
  561. /* send out remaining buffer */
  562. if (pBundleBuffer != qdf_nbuf_data(bundleBuf)) {
  563. htc_handle_extra_tx_credit(pEndpoint, p_last_htc_pkt,
  564. p_last_pkt_bundle_buffer,
  565. &pBundleBuffer,
  566. pBundleBuffer -
  567. qdf_nbuf_data(bundleBuf));
  568. htc_send_bundled_netbuf(target, pEndpoint,
  569. pBundleBuffer - last_credit_pad,
  570. pPacketTx);
  571. } else {
  572. free_htc_bundle_packet(target, pPacketTx);
  573. }
  574. }
  575. #endif /* ENABLE_BUNDLE_TX */
  576. #else
  577. static int htc_tx_pad_credit_avail(HTC_ENDPOINT *ep)
  578. {
  579. return 1;
  580. }
  581. bool htc_handle_extra_tx_credit(HTC_ENDPOINT *ep,
  582. HTC_PACKET *p_last_htc_pkt,
  583. unsigned char *p_last_pkt_bundle_buffer,
  584. unsigned char **p_bundle_buffer,
  585. int tot_data_len);
  586. bool htc_handle_extra_tx_credit(HTC_ENDPOINT *ep,
  587. HTC_PACKET *p_last_htc_pkt,
  588. unsigned char *p_last_pkt_bundle_buffer,
  589. unsigned char **p_bundle_buffer,
  590. int tot_data_len)
  591. {
  592. return FALSE;
  593. }
  594. static void htc_issue_packets_bundle(HTC_TARGET *target,
  595. HTC_ENDPOINT *pEndpoint,
  596. HTC_PACKET_QUEUE *pPktQueue)
  597. {
  598. }
  599. #endif
  600. /**
  601. * htc_issue_packets() - HTC function to send packets from a queue
  602. * @target: HTC target on which packets need to be sent
  603. * @pEndpoint: logical endpoint on which packets needs to be sent
  604. * @pPktQueue: HTC packet queue containing the list of packets to be sent
  605. *
  606. * Return: QDF_STATUS_SUCCESS on success and error QDF status on failure
  607. */
  608. static QDF_STATUS htc_issue_packets(HTC_TARGET *target,
  609. HTC_ENDPOINT *pEndpoint,
  610. HTC_PACKET_QUEUE *pPktQueue)
  611. {
  612. QDF_STATUS status = QDF_STATUS_SUCCESS;
  613. qdf_nbuf_t netbuf;
  614. HTC_PACKET *pPacket = NULL;
  615. uint16_t payloadLen;
  616. HTC_FRAME_HDR *pHtcHdr;
  617. uint32_t data_attr = 0;
  618. enum qdf_bus_type bus_type;
  619. QDF_STATUS ret;
  620. bool rt_put = false;
  621. bool used_extra_tx_credit = false;
  622. uint8_t *buf = NULL;
  623. int (*update_ep_padding_credit)(void *, int);
  624. void *ctx = NULL;
  625. update_ep_padding_credit =
  626. pEndpoint->EpCallBacks.ep_padding_credit_update;
  627. bus_type = hif_get_bus_type(target->hif_dev);
  628. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  629. ("+htc_issue_packets: Queue: %pK, Pkts %d\n", pPktQueue,
  630. HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
  631. while (true) {
  632. if (HTC_TX_BUNDLE_ENABLED(target) &&
  633. HTC_PACKET_QUEUE_DEPTH(pPktQueue) >=
  634. HTC_MIN_MSG_PER_BUNDLE) {
  635. switch (bus_type) {
  636. case QDF_BUS_TYPE_SDIO:
  637. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
  638. break;
  639. if (update_ep_padding_credit) {
  640. if (htc_tx_pad_credit_avail
  641. (pEndpoint) < 1)
  642. break;
  643. }
  644. case QDF_BUS_TYPE_USB:
  645. htc_issue_packets_bundle(target,
  646. pEndpoint,
  647. pPktQueue);
  648. break;
  649. default:
  650. break;
  651. }
  652. }
  653. /* if not bundling or there was a packet that could not be
  654. * placed in a bundle, and send it by normal way
  655. */
  656. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  657. if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
  658. status = QDF_STATUS_E_FAILURE;
  659. break;
  660. }
  661. }
  662. pPacket = htc_packet_dequeue(pPktQueue);
  663. if (!pPacket) {
  664. /* local queue is fully drained */
  665. break;
  666. }
  667. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  668. AR_DEBUG_ASSERT(netbuf);
  669. /* Non-credit enabled endpoints have been mapped and setup by
  670. * now, so no need to revisit the HTC headers
  671. */
  672. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  673. payloadLen = pPacket->ActualLength;
  674. /* setup HTC frame header */
  675. pHtcHdr = (HTC_FRAME_HDR *)
  676. qdf_nbuf_get_frag_vaddr(netbuf, 0);
  677. if (qdf_unlikely(!pHtcHdr)) {
  678. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  679. ("%s Invalid pHtcHdr\n",
  680. __func__));
  681. AR_DEBUG_ASSERT(pHtcHdr);
  682. status = QDF_STATUS_E_FAILURE;
  683. break;
  684. }
  685. HTC_WRITE32(pHtcHdr,
  686. SM(payloadLen,
  687. HTC_FRAME_HDR_PAYLOADLEN) |
  688. SM(pPacket->PktInfo.AsTx.SendFlags,
  689. HTC_FRAME_HDR_FLAGS) |
  690. SM(pPacket->Endpoint,
  691. HTC_FRAME_HDR_ENDPOINTID));
  692. HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
  693. SM(pPacket->PktInfo.AsTx.SeqNo,
  694. HTC_FRAME_HDR_CONTROLBYTES1));
  695. /*
  696. * Now that the HTC frame header has been added, the
  697. * netbuf can be mapped. This only applies to non-data
  698. * frames, since data frames were already mapped as they
  699. * entered into the driver.
  700. */
  701. pPacket->PktInfo.AsTx.Flags |=
  702. HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  703. ret = qdf_nbuf_map(target->osdev,
  704. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  705. QDF_DMA_TO_DEVICE);
  706. if (ret != QDF_STATUS_SUCCESS) {
  707. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  708. ("%s nbuf Map Fail Endpnt %pK\n",
  709. __func__, pEndpoint));
  710. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  711. status = QDF_STATUS_E_FAILURE;
  712. break;
  713. }
  714. }
  715. if (!pEndpoint->async_update) {
  716. LOCK_HTC_TX(target);
  717. }
  718. /* store in look up queue to match completions */
  719. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  720. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  721. pEndpoint->ul_outstanding_cnt++;
  722. if (!pEndpoint->async_update) {
  723. UNLOCK_HTC_TX(target);
  724. hif_send_complete_check(target->hif_dev,
  725. pEndpoint->UL_PipeID, false);
  726. }
  727. htc_packet_set_magic_cookie(pPacket, HTC_PACKET_MAGIC_COOKIE);
  728. /*
  729. * For HTT messages without a response from fw,
  730. * do the runtime put here.
  731. * otherwise runtime put will be done when the fw response comes
  732. */
  733. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_RUNTIME_PUT)
  734. rt_put = true;
  735. #if DEBUG_BUNDLE
  736. qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
  737. pEndpoint->Id,
  738. pEndpoint->TxCreditSize,
  739. HTC_HDR_LENGTH + pPacket->ActualLength);
  740. #endif
  741. buf = (uint8_t *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  742. used_extra_tx_credit =
  743. htc_handle_extra_tx_credit(pEndpoint, pPacket, buf,
  744. NULL, pPacket->ActualLength +
  745. HTC_HDR_LENGTH);
  746. status = hif_send_head(target->hif_dev,
  747. pEndpoint->UL_PipeID, pEndpoint->Id,
  748. HTC_HDR_LENGTH + pPacket->ActualLength,
  749. netbuf, data_attr);
  750. if (status != QDF_STATUS_SUCCESS) {
  751. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  752. if (used_extra_tx_credit) {
  753. ctx = pEndpoint->EpCallBacks.pContext;
  754. update_ep_padding_credit(ctx, 1);
  755. }
  756. }
  757. }
  758. htc_issue_tx_bundle_stats_inc(target);
  759. target->ce_send_cnt++;
  760. if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
  761. if (status != QDF_STATUS_E_RESOURCES) {
  762. /* TODO : if more than 1 endpoint maps to the
  763. * same PipeID it is possible to run out of
  764. * resources in the HIF layer. Don't emit the
  765. * error
  766. */
  767. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  768. ("hif_send Failed status:%d\n",
  769. status));
  770. }
  771. /* only unmap if we mapped in this function */
  772. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  773. qdf_nbuf_unmap(target->osdev,
  774. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  775. QDF_DMA_TO_DEVICE);
  776. pPacket->PktInfo.AsTx.Flags &=
  777. ~HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  778. }
  779. if (!pEndpoint->async_update) {
  780. LOCK_HTC_TX(target);
  781. }
  782. target->ce_send_cnt--;
  783. pEndpoint->ul_outstanding_cnt--;
  784. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  785. htc_packet_set_magic_cookie(pPacket, 0);
  786. /* put it back into the callers queue */
  787. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  788. /* reclaim credits */
  789. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue,
  790. pPacket) {
  791. pEndpoint->TxCredits +=
  792. pPacket->PktInfo.AsTx.CreditsUsed;
  793. } HTC_PACKET_QUEUE_ITERATE_END;
  794. if (!pEndpoint->async_update) {
  795. UNLOCK_HTC_TX(target);
  796. }
  797. break;
  798. }
  799. if (rt_put) {
  800. hif_pm_runtime_put(target->hif_dev);
  801. rt_put = false;
  802. }
  803. }
  804. if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
  805. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  806. ("htc_issue_packets, failed pkt:0x%pK status:%d",
  807. pPacket, status));
  808. }
  809. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_issue_packets\n"));
  810. return status;
  811. }
  812. #ifdef FEATURE_RUNTIME_PM
  813. /**
  814. * extract_htc_pm_packets(): move pm packets from endpoint into queue
  815. * @endpoint: which enpoint to extract packets from
  816. * @queue: a queue to store extracted packets in.
  817. *
  818. * remove pm packets from the endpoint's tx queue.
  819. * queue them into a queue
  820. */
  821. static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
  822. HTC_PACKET_QUEUE *queue)
  823. {
  824. HTC_PACKET *packet;
  825. /* only WMI endpoint has power management packets */
  826. if (endpoint->service_id != WMI_CONTROL_SVC)
  827. return;
  828. ITERATE_OVER_LIST_ALLOW_REMOVE(&endpoint->TxQueue.QueueHead, packet,
  829. HTC_PACKET, ListLink) {
  830. if (packet->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_AUTO_PM) {
  831. HTC_PACKET_REMOVE(&endpoint->TxQueue, packet);
  832. HTC_PACKET_ENQUEUE(queue, packet);
  833. }
  834. } ITERATE_END
  835. }
  836. /**
  837. * queue_htc_pm_packets(): queue pm packets with priority
  838. * @endpoint: enpoint to queue packets to
  839. * @queue: queue of pm packets to enque
  840. *
  841. * suspend resume packets get special treatment & priority.
  842. * need to queue them at the front of the queue.
  843. */
  844. static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
  845. HTC_PACKET_QUEUE *queue)
  846. {
  847. if (endpoint->service_id != WMI_CONTROL_SVC)
  848. return;
  849. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&endpoint->TxQueue, queue);
  850. }
  851. #else
  852. static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
  853. HTC_PACKET_QUEUE *queue)
  854. {}
  855. static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
  856. HTC_PACKET_QUEUE *queue)
  857. {}
  858. #endif
  859. /**
  860. * get_htc_send_packets_credit_based() - get packets based on available credits
  861. * @target: HTC target on which packets need to be sent
  862. * @pEndpoint: logical endpoint on which packets needs to be sent
  863. * @pQueue: HTC packet queue containing the list of packets to be sent
  864. *
  865. * Get HTC send packets from TX queue on an endpoint based on available credits.
  866. * The function moves the packets from TX queue of the endpoint to pQueue.
  867. *
  868. * Return: None
  869. */
  870. static void get_htc_send_packets_credit_based(HTC_TARGET *target,
  871. HTC_ENDPOINT *pEndpoint,
  872. HTC_PACKET_QUEUE *pQueue)
  873. {
  874. int creditsRequired;
  875. int remainder;
  876. uint8_t sendFlags;
  877. HTC_PACKET *pPacket;
  878. unsigned int transferLength;
  879. HTC_PACKET_QUEUE *tx_queue;
  880. HTC_PACKET_QUEUE pm_queue;
  881. bool do_pm_get = false;
  882. int ret;
  883. /*** NOTE : the TX lock is held when this function is called ***/
  884. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  885. ("+get_htc_send_packets_credit_based\n"));
  886. INIT_HTC_PACKET_QUEUE(&pm_queue);
  887. extract_htc_pm_packets(pEndpoint, &pm_queue);
  888. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  889. tx_queue = &pEndpoint->TxQueue;
  890. do_pm_get = true;
  891. } else {
  892. tx_queue = &pm_queue;
  893. }
  894. /* loop until we can grab as many packets out of the queue as we can */
  895. while (true) {
  896. if (do_pm_get) {
  897. ret = hif_pm_runtime_get(target->hif_dev);
  898. if (ret) {
  899. /* bus suspended, runtime resume issued */
  900. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  901. if (ret == -EAGAIN) {
  902. pPacket = htc_get_pkt_at_head(tx_queue);
  903. if (!pPacket)
  904. break;
  905. log_packet_info(target, pPacket);
  906. }
  907. break;
  908. }
  909. }
  910. sendFlags = 0;
  911. /* get packet at head, but don't remove it */
  912. pPacket = htc_get_pkt_at_head(tx_queue);
  913. if (!pPacket) {
  914. if (do_pm_get)
  915. hif_pm_runtime_put(target->hif_dev);
  916. break;
  917. }
  918. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  919. (" Got head packet:%pK , Queue Depth: %d\n",
  920. pPacket,
  921. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  922. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  923. if (transferLength <= pEndpoint->TxCreditSize) {
  924. creditsRequired = 1;
  925. } else {
  926. /* figure out how many credits this message requires */
  927. creditsRequired =
  928. transferLength / pEndpoint->TxCreditSize;
  929. remainder = transferLength % pEndpoint->TxCreditSize;
  930. if (remainder)
  931. creditsRequired++;
  932. }
  933. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  934. (" Credits Required:%d Got:%d\n",
  935. creditsRequired, pEndpoint->TxCredits));
  936. if (pEndpoint->Id == ENDPOINT_0) {
  937. /*
  938. * endpoint 0 is special, it always has a credit and
  939. * does not require credit based flow control
  940. */
  941. creditsRequired = 0;
  942. } else {
  943. if (pEndpoint->TxCredits < creditsRequired) {
  944. #if DEBUG_CREDIT
  945. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  946. ("EP%d,No Credit now.%d < %d\n",
  947. pEndpoint->Id,
  948. pEndpoint->TxCredits,
  949. creditsRequired));
  950. #endif
  951. if (do_pm_get)
  952. hif_pm_runtime_put(target->hif_dev);
  953. break;
  954. }
  955. pEndpoint->TxCredits -= creditsRequired;
  956. INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
  957. creditsRequired);
  958. /* check if we need credits back from the target */
  959. if (pEndpoint->TxCredits <=
  960. pEndpoint->TxCreditsPerMaxMsg) {
  961. /* tell the target we need credits ASAP! */
  962. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  963. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  964. htc_credit_record(HTC_REQUEST_CREDIT,
  965. pEndpoint->TxCredits,
  966. HTC_PACKET_QUEUE_DEPTH
  967. (tx_queue));
  968. }
  969. INC_HTC_EP_STAT(pEndpoint,
  970. TxCreditLowIndications, 1);
  971. #if DEBUG_CREDIT
  972. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  973. (" EP%d Needs Credits\n",
  974. pEndpoint->Id));
  975. #endif
  976. }
  977. }
  978. /* now we can fully dequeue */
  979. pPacket = htc_packet_dequeue(tx_queue);
  980. if (pPacket) {
  981. /* save the number of credits this packet consumed */
  982. pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
  983. /* save send flags */
  984. pPacket->PktInfo.AsTx.SendFlags = sendFlags;
  985. /* queue this packet into the caller's queue */
  986. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  987. }
  988. }
  989. if (!HTC_QUEUE_EMPTY(&pm_queue))
  990. queue_htc_pm_packets(pEndpoint, &pm_queue);
  991. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  992. ("-get_htc_send_packets_credit_based\n"));
  993. }
  994. static void get_htc_send_packets(HTC_TARGET *target,
  995. HTC_ENDPOINT *pEndpoint,
  996. HTC_PACKET_QUEUE *pQueue, int Resources)
  997. {
  998. HTC_PACKET *pPacket;
  999. HTC_PACKET_QUEUE *tx_queue;
  1000. HTC_PACKET_QUEUE pm_queue;
  1001. bool do_pm_get = false;
  1002. int ret;
  1003. /*** NOTE : the TX lock is held when this function is called ***/
  1004. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1005. ("+get_htc_send_packets %d resources\n", Resources));
  1006. INIT_HTC_PACKET_QUEUE(&pm_queue);
  1007. extract_htc_pm_packets(pEndpoint, &pm_queue);
  1008. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  1009. tx_queue = &pEndpoint->TxQueue;
  1010. do_pm_get = true;
  1011. } else {
  1012. tx_queue = &pm_queue;
  1013. }
  1014. /* loop until we can grab as many packets out of the queue as we can */
  1015. while (Resources > 0) {
  1016. int num_frags;
  1017. if (do_pm_get) {
  1018. ret = hif_pm_runtime_get(target->hif_dev);
  1019. if (ret) {
  1020. /* bus suspended, runtime resume issued */
  1021. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  1022. if (ret == -EAGAIN) {
  1023. pPacket = htc_get_pkt_at_head(tx_queue);
  1024. if (!pPacket)
  1025. break;
  1026. log_packet_info(target, pPacket);
  1027. }
  1028. break;
  1029. }
  1030. }
  1031. pPacket = htc_packet_dequeue(tx_queue);
  1032. if (!pPacket) {
  1033. if (do_pm_get)
  1034. hif_pm_runtime_put(target->hif_dev);
  1035. break;
  1036. }
  1037. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1038. (" Got packet:%pK , New Queue Depth: %d\n",
  1039. pPacket,
  1040. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  1041. /* For non-credit path the sequence number is already embedded
  1042. * in the constructed HTC header
  1043. */
  1044. pPacket->PktInfo.AsTx.SendFlags = 0;
  1045. pPacket->PktInfo.AsTx.CreditsUsed = 0;
  1046. /* queue this packet into the caller's queue */
  1047. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  1048. /*
  1049. * FIX THIS:
  1050. * For now, avoid calling qdf_nbuf_get_num_frags before calling
  1051. * qdf_nbuf_map, because the MacOS version of qdf_nbuf_t doesn't
  1052. * support qdf_nbuf_get_num_frags until after qdf_nbuf_map has
  1053. * been done.
  1054. * Assume that the non-data netbufs, i.e. WMI message netbufs,
  1055. * consist of a single fragment.
  1056. */
  1057. /* WMI messages are in a single-fragment network buf */
  1058. num_frags =
  1059. (pPacket->PktInfo.AsTx.
  1060. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1 :
  1061. qdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
  1062. (pPacket));
  1063. Resources -= num_frags;
  1064. }
  1065. if (!HTC_QUEUE_EMPTY(&pm_queue))
  1066. queue_htc_pm_packets(pEndpoint, &pm_queue);
  1067. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets\n"));
  1068. }
  1069. /**
  1070. * htc_try_send() - Send packets in a queue on an endpoint
  1071. * @target: HTC target on which packets need to be sent
  1072. * @pEndpoint: logical endpoint on which packets needs to be sent
  1073. * @pCallersSendQueue: packet queue containing the list of packets to be sent
  1074. *
  1075. * Return: enum HTC_SEND_QUEUE_RESULT indicates whether the packet was queued to
  1076. * be sent or the packet should be dropped by the upper layer
  1077. */
  1078. static enum HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
  1079. HTC_ENDPOINT *pEndpoint,
  1080. HTC_PACKET_QUEUE *pCallersSendQueue)
  1081. {
  1082. /* temp queue to hold packets at various stages */
  1083. HTC_PACKET_QUEUE sendQueue;
  1084. HTC_PACKET *pPacket;
  1085. int tx_resources;
  1086. int overflow;
  1087. enum HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
  1088. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%pK Depth:%d)\n",
  1089. pCallersSendQueue,
  1090. (pCallersSendQueue ==
  1091. NULL) ? 0 :
  1092. HTC_PACKET_QUEUE_DEPTH
  1093. (pCallersSendQueue)));
  1094. /* init the local send queue */
  1095. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1096. do {
  1097. /* caller didn't provide a queue, just wants us to check
  1098. * queues and send
  1099. */
  1100. if (!pCallersSendQueue)
  1101. break;
  1102. if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
  1103. /* empty queue */
  1104. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  1105. HTC_PKT_Q_EMPTY);
  1106. result = HTC_SEND_QUEUE_DROP;
  1107. break;
  1108. }
  1109. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
  1110. pEndpoint->MaxTxQueueDepth) {
  1111. /* we've already overflowed */
  1112. overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  1113. } else {
  1114. /* figure out how much we will overflow by */
  1115. overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  1116. overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  1117. /* get how much we will overflow the TX queue by */
  1118. overflow -= pEndpoint->MaxTxQueueDepth;
  1119. }
  1120. /* if overflow is negative or zero, we are okay */
  1121. if (overflow > 0) {
  1122. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1123. ("Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d\n",
  1124. pEndpoint->Id, overflow,
  1125. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  1126. TxQueue),
  1127. pEndpoint->MaxTxQueueDepth));
  1128. }
  1129. if ((overflow <= 0)
  1130. || (!pEndpoint->EpCallBacks.EpSendFull)) {
  1131. /* all packets will fit or caller did not provide send
  1132. * full indication handler
  1133. * just move all of them to local sendQueue object
  1134. */
  1135. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
  1136. pCallersSendQueue);
  1137. } else {
  1138. int i;
  1139. int goodPkts =
  1140. HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
  1141. overflow;
  1142. A_ASSERT(goodPkts >= 0);
  1143. /* we have overflowed and callback is provided. Dequeue
  1144. * all non-overflow packets into the sendqueue
  1145. */
  1146. for (i = 0; i < goodPkts; i++) {
  1147. /* pop off caller's queue */
  1148. pPacket = htc_packet_dequeue(pCallersSendQueue);
  1149. A_ASSERT(pPacket);
  1150. if (pPacket)
  1151. /* insert into local queue */
  1152. HTC_PACKET_ENQUEUE(&sendQueue,
  1153. pPacket);
  1154. }
  1155. /* the caller's queue has all the packets that won't fit
  1156. * walk through the caller's queue and indicate each one
  1157. * to the send full handler
  1158. */
  1159. ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
  1160. QueueHead, pPacket,
  1161. HTC_PACKET, ListLink) {
  1162. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1163. ("Indicating overflowed TX packet: %pK\n",
  1164. pPacket));
  1165. /*
  1166. * Remove headroom reserved for HTC_FRAME_HDR
  1167. * before giving the packet back to the user via
  1168. * the EpSendFull callback.
  1169. */
  1170. restore_tx_packet(target, pPacket);
  1171. if (pEndpoint->EpCallBacks.
  1172. EpSendFull(pEndpoint->EpCallBacks.pContext,
  1173. pPacket) == HTC_SEND_FULL_DROP) {
  1174. /* callback wants the packet dropped */
  1175. INC_HTC_EP_STAT(pEndpoint, TxDropped,
  1176. 1);
  1177. /* leave this one in the caller's queue
  1178. * for cleanup
  1179. */
  1180. } else {
  1181. /* callback wants to keep this packet,
  1182. * remove from caller's queue
  1183. */
  1184. HTC_PACKET_REMOVE(pCallersSendQueue,
  1185. pPacket);
  1186. /* put it in the send queue
  1187. * add HTC_FRAME_HDR space reservation
  1188. * again
  1189. */
  1190. qdf_nbuf_push_head
  1191. (GET_HTC_PACKET_NET_BUF_CONTEXT
  1192. (pPacket),
  1193. sizeof(HTC_FRAME_HDR));
  1194. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  1195. }
  1196. }
  1197. ITERATE_END;
  1198. if (HTC_QUEUE_EMPTY(&sendQueue)) {
  1199. /* no packets made it in, caller will cleanup */
  1200. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  1201. HTC_SEND_Q_EMPTY);
  1202. result = HTC_SEND_QUEUE_DROP;
  1203. break;
  1204. }
  1205. }
  1206. } while (false);
  1207. if (result != HTC_SEND_QUEUE_OK) {
  1208. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: %d\n",
  1209. result));
  1210. return result;
  1211. }
  1212. LOCK_HTC_TX(target);
  1213. if (!HTC_QUEUE_EMPTY(&sendQueue)) {
  1214. if (target->is_nodrop_pkt) {
  1215. /*
  1216. * nodrop pkts have higher priority than normal pkts,
  1217. * insert nodrop pkt to head for proper
  1218. * start/termination of test.
  1219. */
  1220. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1221. &sendQueue);
  1222. target->is_nodrop_pkt = false;
  1223. } else {
  1224. /* transfer packets to tail */
  1225. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
  1226. &sendQueue);
  1227. A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
  1228. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1229. }
  1230. }
  1231. /* increment tx processing count on entry */
  1232. if (qdf_atomic_inc_return(&pEndpoint->TxProcessCount) > 1) {
  1233. /* another thread or task is draining the TX queues on this
  1234. * endpoint that thread will reset the tx processing count when
  1235. * the queue is drained
  1236. */
  1237. qdf_atomic_dec(&pEndpoint->TxProcessCount);
  1238. UNLOCK_HTC_TX(target);
  1239. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy)\n"));
  1240. return HTC_SEND_QUEUE_OK;
  1241. }
  1242. /***** beyond this point only 1 thread may enter ******/
  1243. /* now drain the endpoint TX queue for transmission as long as we have
  1244. * enough transmit resources
  1245. */
  1246. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1247. tx_resources =
  1248. hif_get_free_queue_number(target->hif_dev,
  1249. pEndpoint->UL_PipeID);
  1250. } else {
  1251. tx_resources = 0;
  1252. }
  1253. while (true) {
  1254. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0)
  1255. break;
  1256. if (pEndpoint->async_update &&
  1257. (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) &&
  1258. (!tx_resources))
  1259. break;
  1260. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1261. #if DEBUG_CREDIT
  1262. int cred = pEndpoint->TxCredits;
  1263. #endif
  1264. /* credit based mechanism provides flow control based on
  1265. * target transmit resource availability, we assume that
  1266. * the HIF layer will always have bus resources greater
  1267. * than target transmit resources
  1268. */
  1269. get_htc_send_packets_credit_based(target, pEndpoint,
  1270. &sendQueue);
  1271. #if DEBUG_CREDIT
  1272. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1273. if (cred - pEndpoint->TxCredits > 0) {
  1274. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1275. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  1276. pEndpoint->Id, cred,
  1277. cred -
  1278. pEndpoint->TxCredits,
  1279. pEndpoint->TxCredits));
  1280. }
  1281. }
  1282. #endif
  1283. } else {
  1284. /*
  1285. * Header and payload belongs to the different fragments and
  1286. * consume 2 resource for one HTC package but USB combine into
  1287. * one transfer.And one WMI message only consumes one single
  1288. * resource.
  1289. */
  1290. if (HTC_TX_BUNDLE_ENABLED(target) && tx_resources &&
  1291. hif_get_bus_type(target->hif_dev) ==
  1292. QDF_BUS_TYPE_USB) {
  1293. if (pEndpoint->service_id ==
  1294. WMI_CONTROL_SVC)
  1295. tx_resources =
  1296. HTC_MAX_MSG_PER_BUNDLE_TX;
  1297. else
  1298. tx_resources =
  1299. (HTC_MAX_MSG_PER_BUNDLE_TX * 2);
  1300. }
  1301. /* get all the packets for this endpoint that we can for
  1302. * this pass
  1303. */
  1304. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1305. tx_resources);
  1306. }
  1307. if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
  1308. /* didn't get any packets due to a lack of resources or
  1309. * TX queue was drained
  1310. */
  1311. break;
  1312. }
  1313. if (!pEndpoint->async_update)
  1314. UNLOCK_HTC_TX(target);
  1315. /* send what we can */
  1316. if (htc_issue_packets(target, pEndpoint, &sendQueue)) {
  1317. int i;
  1318. result = HTC_SEND_QUEUE_DROP;
  1319. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1320. ("htc_issue_packets, failed status:%d put it back to head of callersSendQueue",
  1321. result));
  1322. for (i = HTC_PACKET_QUEUE_DEPTH(&sendQueue); i > 0; i--)
  1323. hif_pm_runtime_put(target->hif_dev);
  1324. if (!pEndpoint->async_update) {
  1325. LOCK_HTC_TX(target);
  1326. }
  1327. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1328. &sendQueue);
  1329. break;
  1330. }
  1331. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1332. tx_resources =
  1333. hif_get_free_queue_number(target->hif_dev,
  1334. pEndpoint->UL_PipeID);
  1335. }
  1336. if (!pEndpoint->async_update) {
  1337. LOCK_HTC_TX(target);
  1338. }
  1339. }
  1340. /* done with this endpoint, we can clear the count */
  1341. qdf_atomic_init(&pEndpoint->TxProcessCount);
  1342. UNLOCK_HTC_TX(target);
  1343. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send:\n"));
  1344. return HTC_SEND_QUEUE_OK;
  1345. }
  1346. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1347. static uint16_t htc_send_pkts_sched_check(HTC_HANDLE HTCHandle,
  1348. HTC_ENDPOINT_ID id)
  1349. {
  1350. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1351. HTC_ENDPOINT *pEndpoint;
  1352. HTC_ENDPOINT_ID eid;
  1353. HTC_PACKET_QUEUE *pTxQueue;
  1354. uint16_t resources;
  1355. uint16_t acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
  1356. if (id < ENDPOINT_2 || id > ENDPOINT_5)
  1357. return 1;
  1358. for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
  1359. pEndpoint = &target->endpoint[eid];
  1360. pTxQueue = &pEndpoint->TxQueue;
  1361. if (HTC_QUEUE_EMPTY(pTxQueue))
  1362. acQueueStatus[eid - 2] = 1;
  1363. }
  1364. switch (id) {
  1365. case ENDPOINT_2: /* BE */
  1366. return acQueueStatus[0] && acQueueStatus[2]
  1367. && acQueueStatus[3];
  1368. case ENDPOINT_3: /* BK */
  1369. return acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
  1370. && acQueueStatus[3];
  1371. case ENDPOINT_4: /* VI */
  1372. return acQueueStatus[2] && acQueueStatus[3];
  1373. case ENDPOINT_5: /* VO */
  1374. return acQueueStatus[3];
  1375. default:
  1376. return 0;
  1377. }
  1378. }
  1379. static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
  1380. HTC_PACKET_QUEUE *pPktQueue,
  1381. HTC_ENDPOINT_ID eid)
  1382. {
  1383. HTC_ENDPOINT *pEndpoint;
  1384. HTC_PACKET_QUEUE *pTxQueue;
  1385. HTC_PACKET *pPacket;
  1386. int goodPkts;
  1387. pEndpoint = &target->endpoint[eid];
  1388. pTxQueue = &pEndpoint->TxQueue;
  1389. LOCK_HTC_TX(target);
  1390. goodPkts =
  1391. pEndpoint->MaxTxQueueDepth -
  1392. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  1393. if (goodPkts > 0) {
  1394. while (!HTC_QUEUE_EMPTY(pPktQueue)) {
  1395. pPacket = htc_packet_dequeue(pPktQueue);
  1396. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  1397. goodPkts--;
  1398. if (goodPkts <= 0)
  1399. break;
  1400. }
  1401. }
  1402. if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
  1403. ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
  1404. HTC_PACKET, ListLink) {
  1405. if (pEndpoint->EpCallBacks.
  1406. EpSendFull(pEndpoint->EpCallBacks.pContext,
  1407. pPacket) == HTC_SEND_FULL_DROP) {
  1408. INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
  1409. } else {
  1410. HTC_PACKET_REMOVE(pPktQueue, pPacket);
  1411. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  1412. }
  1413. }
  1414. ITERATE_END;
  1415. }
  1416. UNLOCK_HTC_TX(target);
  1417. return A_OK;
  1418. }
  1419. #endif
  1420. static inline QDF_STATUS __htc_send_pkt(HTC_HANDLE HTCHandle,
  1421. HTC_PACKET *pPacket)
  1422. {
  1423. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1424. HTC_ENDPOINT *pEndpoint;
  1425. HTC_PACKET_QUEUE pPktQueue;
  1426. qdf_nbuf_t netbuf;
  1427. HTC_FRAME_HDR *htc_hdr;
  1428. QDF_STATUS status;
  1429. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1430. ("+__htc_send_pkt\n"));
  1431. /* get packet at head to figure out which endpoint these packets will
  1432. * go into
  1433. */
  1434. if (!pPacket) {
  1435. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
  1436. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
  1437. return QDF_STATUS_E_INVAL;
  1438. }
  1439. if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
  1440. (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
  1441. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1442. ("%s endpoint is invalid\n", __func__));
  1443. AR_DEBUG_ASSERT(0);
  1444. return QDF_STATUS_E_INVAL;
  1445. }
  1446. pEndpoint = &target->endpoint[pPacket->Endpoint];
  1447. if (!pEndpoint->service_id) {
  1448. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
  1449. __func__));
  1450. return QDF_STATUS_E_INVAL;
  1451. }
  1452. #ifdef HTC_EP_STAT_PROFILING
  1453. LOCK_HTC_TX(target);
  1454. INC_HTC_EP_STAT(pEndpoint, TxPosted, 1);
  1455. UNLOCK_HTC_TX(target);
  1456. #endif
  1457. /* provide room in each packet's netbuf for the HTC frame header */
  1458. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1459. AR_DEBUG_ASSERT(netbuf);
  1460. if (!netbuf)
  1461. return QDF_STATUS_E_INVAL;
  1462. qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
  1463. pPacket->PktInfo.AsTx.Flags |=
  1464. HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA;
  1465. /* setup HTC frame header */
  1466. htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1467. AR_DEBUG_ASSERT(htc_hdr);
  1468. if (!htc_hdr)
  1469. return QDF_STATUS_E_INVAL;
  1470. HTC_WRITE32(htc_hdr,
  1471. SM(pPacket->ActualLength,
  1472. HTC_FRAME_HDR_PAYLOADLEN) |
  1473. SM(pPacket->Endpoint,
  1474. HTC_FRAME_HDR_ENDPOINTID));
  1475. LOCK_HTC_TX(target);
  1476. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1477. pEndpoint->SeqNo++;
  1478. HTC_WRITE32(((uint32_t *)htc_hdr) + 1,
  1479. SM(pPacket->PktInfo.AsTx.SeqNo,
  1480. HTC_FRAME_HDR_CONTROLBYTES1));
  1481. UNLOCK_HTC_TX(target);
  1482. /*
  1483. * For flow control enabled endpoints mapping is done in
  1484. * htc_issue_packets and for non flow control enabled endpoints
  1485. * its done here.
  1486. */
  1487. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1488. pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  1489. status = qdf_nbuf_map(target->osdev,
  1490. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  1491. QDF_DMA_TO_DEVICE);
  1492. if (status != QDF_STATUS_SUCCESS) {
  1493. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1494. ("%s: nbuf map failed, endpoint %pK, seq_no. %d\n",
  1495. __func__, pEndpoint, pEndpoint->SeqNo));
  1496. return status;
  1497. }
  1498. }
  1499. INIT_HTC_PACKET_QUEUE_AND_ADD(&pPktQueue, pPacket);
  1500. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1501. if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id))
  1502. htc_send_pkts_sched_queue(HTCHandle, &pPktQueue, pEndpoint->Id);
  1503. else
  1504. htc_try_send(target, pEndpoint, &pPktQueue);
  1505. #else
  1506. htc_try_send(target, pEndpoint, &pPktQueue);
  1507. #endif
  1508. /* do completion on any packets that couldn't get in */
  1509. while (!HTC_QUEUE_EMPTY(&pPktQueue)) {
  1510. pPacket = htc_packet_dequeue(&pPktQueue);
  1511. if (HTC_STOPPING(target))
  1512. pPacket->Status = QDF_STATUS_E_CANCELED;
  1513. else
  1514. pPacket->Status = QDF_STATUS_E_RESOURCES;
  1515. send_packet_completion(target, pPacket);
  1516. }
  1517. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-__htc_send_pkt\n"));
  1518. return QDF_STATUS_SUCCESS;
  1519. }
  1520. /* HTC API - htc_send_pkt */
  1521. QDF_STATUS htc_send_pkt(HTC_HANDLE htc_handle, HTC_PACKET *htc_packet)
  1522. {
  1523. if (!htc_handle) {
  1524. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1525. ("%s: HTCHandle is NULL \n", __func__));
  1526. return QDF_STATUS_E_FAILURE;
  1527. }
  1528. if (!htc_packet) {
  1529. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1530. ("%s: pPacket is NULL \n", __func__));
  1531. return QDF_STATUS_E_FAILURE;
  1532. }
  1533. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1534. ("+-htc_send_pkt: Enter endPointId: %d, buffer: %pK, length: %d\n",
  1535. htc_packet->Endpoint, htc_packet->pBuffer,
  1536. htc_packet->ActualLength));
  1537. return __htc_send_pkt(htc_handle, htc_packet);
  1538. }
  1539. qdf_export_symbol(htc_send_pkt);
  1540. #ifdef ATH_11AC_TXCOMPACT
  1541. /**
  1542. * htc_send_data_pkt() - send single data packet on an endpoint
  1543. * @HTCHandle: pointer to HTC handle
  1544. * @netbuf: network buffer containing the data to be sent
  1545. * @ActualLength: length of data that needs to be transmitted
  1546. *
  1547. * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
  1548. */
  1549. QDF_STATUS htc_send_data_pkt(HTC_HANDLE htc_hdl, qdf_nbuf_t netbuf, int ep_id,
  1550. int actual_length)
  1551. {
  1552. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
  1553. HTC_ENDPOINT *pEndpoint;
  1554. HTC_FRAME_HDR *p_htc_hdr;
  1555. QDF_STATUS status = QDF_STATUS_SUCCESS;
  1556. int tx_resources;
  1557. uint32_t data_attr = 0;
  1558. int htc_payload_len = actual_length;
  1559. pEndpoint = &target->endpoint[ep_id];
  1560. tx_resources = hif_get_free_queue_number(target->hif_dev,
  1561. pEndpoint->UL_PipeID);
  1562. if (tx_resources < HTC_DATA_RESOURCE_THRS) {
  1563. if (pEndpoint->ul_is_polled) {
  1564. hif_send_complete_check(pEndpoint->target->hif_dev,
  1565. pEndpoint->UL_PipeID, 1);
  1566. tx_resources =
  1567. hif_get_free_queue_number(target->hif_dev,
  1568. pEndpoint->UL_PipeID);
  1569. }
  1570. if (tx_resources < HTC_DATA_MINDESC_PERPACKET)
  1571. return QDF_STATUS_E_FAILURE;
  1572. }
  1573. if (hif_pm_runtime_get(target->hif_dev))
  1574. return QDF_STATUS_E_FAILURE;
  1575. p_htc_hdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1576. AR_DEBUG_ASSERT(p_htc_hdr);
  1577. data_attr = qdf_nbuf_data_attr_get(netbuf);
  1578. if (target->htc_hdr_length_check)
  1579. htc_payload_len = actual_length - HTC_HEADER_LEN;
  1580. HTC_WRITE32(p_htc_hdr, SM(htc_payload_len, HTC_FRAME_HDR_PAYLOADLEN)
  1581. | SM(ep_id, HTC_FRAME_HDR_ENDPOINTID));
  1582. /*
  1583. * If the HIF pipe for the data endpoint is polled rather than
  1584. * interrupt-driven, this is a good point to check whether any
  1585. * data previously sent through the HIF pipe have finished being
  1586. * sent.
  1587. * Since this may result in callbacks to htc_tx_completion_handler,
  1588. * which can take the HTC tx lock, make the hif_send_complete_check
  1589. * call before acquiring the HTC tx lock.
  1590. * Call hif_send_complete_check directly, rather than calling
  1591. * htc_send_complete_check, and call the PollTimerStart separately
  1592. * after calling hif_send_head, so the timer will be started to
  1593. * check for completion of the new outstanding download (in the
  1594. * unexpected event that other polling calls don't catch it).
  1595. */
  1596. LOCK_HTC_TX(target);
  1597. HTC_WRITE32(((uint32_t *)p_htc_hdr) + 1,
  1598. SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
  1599. pEndpoint->SeqNo++;
  1600. QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
  1601. DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1602. QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
  1603. sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
  1604. status = hif_send_head(target->hif_dev,
  1605. pEndpoint->UL_PipeID,
  1606. pEndpoint->Id, actual_length, netbuf, data_attr);
  1607. UNLOCK_HTC_TX(target);
  1608. return status;
  1609. }
  1610. #else /*ATH_11AC_TXCOMPACT */
  1611. /**
  1612. * htc_send_data_pkt() - htc_send_data_pkt
  1613. * @HTCHandle: pointer to HTC handle
  1614. * @pPacket: pointer to HTC_PACKET
  1615. * @more_data: indicates whether more data is to follow
  1616. *
  1617. * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
  1618. */
  1619. QDF_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
  1620. uint8_t more_data)
  1621. {
  1622. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1623. HTC_ENDPOINT *pEndpoint;
  1624. HTC_FRAME_HDR *pHtcHdr;
  1625. HTC_PACKET_QUEUE sendQueue;
  1626. qdf_nbuf_t netbuf = NULL;
  1627. int tx_resources;
  1628. QDF_STATUS status = QDF_STATUS_SUCCESS;
  1629. uint32_t data_attr = 0;
  1630. bool used_extra_tx_credit = false;
  1631. if (pPacket) {
  1632. if ((pPacket->Endpoint >= ENDPOINT_MAX) ||
  1633. (pPacket->Endpoint <= ENDPOINT_UNUSED)) {
  1634. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1635. ("%s endpoint is invalid\n", __func__));
  1636. AR_DEBUG_ASSERT(0);
  1637. return QDF_STATUS_E_INVAL;
  1638. }
  1639. pEndpoint = &target->endpoint[pPacket->Endpoint];
  1640. /* add HTC_FRAME_HDR in the initial fragment */
  1641. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1642. pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1643. AR_DEBUG_ASSERT(pHtcHdr);
  1644. HTC_WRITE32(pHtcHdr,
  1645. SM(pPacket->ActualLength,
  1646. HTC_FRAME_HDR_PAYLOADLEN) |
  1647. SM(pPacket->PktInfo.AsTx.SendFlags,
  1648. HTC_FRAME_HDR_FLAGS) |
  1649. SM(pPacket->Endpoint,
  1650. HTC_FRAME_HDR_ENDPOINTID));
  1651. /*
  1652. * If the HIF pipe for the data endpoint is polled rather than
  1653. * interrupt-driven, this is a good point to check whether any
  1654. * data previously sent through the HIF pipe have finished being
  1655. * sent. Since this may result in callbacks to
  1656. * htc_tx_completion_handler, which can take the HTC tx lock,
  1657. * make the hif_send_complete_check call before acquiring the
  1658. * HTC tx lock.
  1659. * Call hif_send_complete_check directly, rather than calling
  1660. * htc_send_complete_check, and call the PollTimerStart
  1661. * separately after calling hif_send_head, so the timer will be
  1662. * started to check for completion of the new outstanding
  1663. * download (in the unexpected event that other polling calls
  1664. * don't catch it).
  1665. */
  1666. if (pEndpoint->ul_is_polled) {
  1667. htc_send_complete_poll_timer_stop(pEndpoint);
  1668. hif_send_complete_check(pEndpoint->target->hif_dev,
  1669. pEndpoint->UL_PipeID, 0);
  1670. }
  1671. LOCK_HTC_TX(target);
  1672. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1673. pEndpoint->SeqNo++;
  1674. HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
  1675. SM(pPacket->PktInfo.AsTx.SeqNo,
  1676. HTC_FRAME_HDR_CONTROLBYTES1));
  1677. /* append new packet to pEndpoint->TxQueue */
  1678. HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
  1679. if (HTC_TX_BUNDLE_ENABLED(target) && (more_data)) {
  1680. UNLOCK_HTC_TX(target);
  1681. return QDF_STATUS_SUCCESS;
  1682. }
  1683. QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
  1684. DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1685. QDF_TRACE_DEFAULT_PDEV_ID, qdf_nbuf_data_addr(netbuf),
  1686. sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
  1687. } else {
  1688. LOCK_HTC_TX(target);
  1689. pEndpoint = &target->endpoint[1];
  1690. }
  1691. /* increment tx processing count on entry */
  1692. qdf_atomic_inc(&pEndpoint->TxProcessCount);
  1693. if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  1694. /*
  1695. * Another thread or task is draining the TX queues on this
  1696. * endpoint. That thread will reset the tx processing count when
  1697. * the queue is drained.
  1698. */
  1699. qdf_atomic_dec(&pEndpoint->TxProcessCount);
  1700. UNLOCK_HTC_TX(target);
  1701. return QDF_STATUS_SUCCESS;
  1702. }
  1703. /***** beyond this point only 1 thread may enter ******/
  1704. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1705. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1706. #if DEBUG_CREDIT
  1707. int cred = pEndpoint->TxCredits;
  1708. #endif
  1709. get_htc_send_packets_credit_based(target, pEndpoint,
  1710. &sendQueue);
  1711. #if DEBUG_CREDIT
  1712. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1713. if (cred - pEndpoint->TxCredits > 0) {
  1714. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1715. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  1716. pEndpoint->Id, cred,
  1717. cred - pEndpoint->TxCredits,
  1718. pEndpoint->TxCredits));
  1719. }
  1720. }
  1721. #endif
  1722. UNLOCK_HTC_TX(target);
  1723. }
  1724. else if (HTC_TX_BUNDLE_ENABLED(target)) {
  1725. if (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB) {
  1726. if (hif_get_free_queue_number(target->hif_dev,
  1727. pEndpoint->UL_PipeID))
  1728. /*
  1729. * Header and payload belongs to the different
  1730. * fragments and consume 2 resource for one HTC
  1731. * package but USB combine into one transfer.
  1732. */
  1733. get_htc_send_packets(target, pEndpoint,
  1734. &sendQueue,
  1735. HTC_MAX_MSG_PER_BUNDLE_TX
  1736. * 2);
  1737. } else {
  1738. /* Dequeue max packets from endpoint tx queue */
  1739. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1740. HTC_MAX_TX_BUNDLE_SEND_LIMIT);
  1741. }
  1742. UNLOCK_HTC_TX(target);
  1743. } else {
  1744. /*
  1745. * Now drain the endpoint TX queue for transmission as long as
  1746. * we have enough transmit resources
  1747. */
  1748. tx_resources =
  1749. hif_get_free_queue_number(target->hif_dev,
  1750. pEndpoint->UL_PipeID);
  1751. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1752. tx_resources);
  1753. UNLOCK_HTC_TX(target);
  1754. }
  1755. /* send what we can */
  1756. while (true) {
  1757. if (HTC_TX_BUNDLE_ENABLED(target) &&
  1758. (HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
  1759. HTC_MIN_MSG_PER_BUNDLE) &&
  1760. (hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_SDIO ||
  1761. hif_get_bus_type(target->hif_dev) == QDF_BUS_TYPE_USB)) {
  1762. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  1763. if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
  1764. status = QDF_STATUS_E_RESOURCES;
  1765. /* put the sendQueue back at the front
  1766. * of pEndpoint->TxQueue
  1767. */
  1768. LOCK_HTC_TX(target);
  1769. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(
  1770. &pEndpoint->TxQueue,
  1771. &sendQueue);
  1772. UNLOCK_HTC_TX(target);
  1773. break;
  1774. }
  1775. }
  1776. htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
  1777. }
  1778. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  1779. if (htc_tx_pad_credit_avail(pEndpoint) < 1) {
  1780. status = QDF_STATUS_E_RESOURCES;
  1781. /* put the sendQueue back at the front
  1782. * of pEndpoint->TxQueue
  1783. */
  1784. LOCK_HTC_TX(target);
  1785. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(
  1786. &pEndpoint->TxQueue,
  1787. &sendQueue);
  1788. UNLOCK_HTC_TX(target);
  1789. break;
  1790. }
  1791. }
  1792. pPacket = htc_packet_dequeue(&sendQueue);
  1793. if (!pPacket)
  1794. break;
  1795. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1796. pHtcHdr = (HTC_FRAME_HDR *)qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1797. LOCK_HTC_TX(target);
  1798. /* store in look up queue to match completions */
  1799. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  1800. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  1801. pEndpoint->ul_outstanding_cnt++;
  1802. UNLOCK_HTC_TX(target);
  1803. used_extra_tx_credit =
  1804. htc_handle_extra_tx_credit(pEndpoint, pPacket,
  1805. (uint8_t *)pHtcHdr,
  1806. NULL,
  1807. pPacket->ActualLength
  1808. + HTC_HDR_LENGTH);
  1809. status = hif_send_head(target->hif_dev,
  1810. pEndpoint->UL_PipeID,
  1811. pEndpoint->Id,
  1812. HTC_HDR_LENGTH + pPacket->ActualLength,
  1813. netbuf, data_attr);
  1814. if (status != QDF_STATUS_SUCCESS) {
  1815. if (pEndpoint->EpCallBacks.ep_padding_credit_update) {
  1816. if (used_extra_tx_credit) {
  1817. pEndpoint->EpCallBacks.
  1818. ep_padding_credit_update
  1819. (pEndpoint->EpCallBacks.pContext, 1);
  1820. }
  1821. }
  1822. }
  1823. #if DEBUG_BUNDLE
  1824. qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.",
  1825. pEndpoint->Id,
  1826. pEndpoint->TxCreditSize,
  1827. HTC_HDR_LENGTH + pPacket->ActualLength);
  1828. #endif
  1829. htc_issue_tx_bundle_stats_inc(target);
  1830. if (qdf_unlikely(QDF_IS_STATUS_ERROR(status))) {
  1831. LOCK_HTC_TX(target);
  1832. pEndpoint->ul_outstanding_cnt--;
  1833. /* remove this packet from the tx completion queue */
  1834. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  1835. /*
  1836. * Don't bother reclaiming credits - HTC flow control
  1837. * is not applicable to tx data.
  1838. * In LL systems, there is no download flow control,
  1839. * since there's virtually no download delay.
  1840. * In HL systems, the txrx SW explicitly performs the
  1841. * tx flow control.
  1842. */
  1843. /* pEndpoint->TxCredits +=
  1844. * pPacket->PktInfo.AsTx.CreditsUsed;
  1845. */
  1846. /* put this frame back at the front of the sendQueue */
  1847. HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
  1848. /* put the sendQueue back at the front of
  1849. * pEndpoint->TxQueue
  1850. */
  1851. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1852. &sendQueue);
  1853. UNLOCK_HTC_TX(target);
  1854. break; /* still need to reset TxProcessCount */
  1855. }
  1856. }
  1857. /* done with this endpoint, we can clear the count */
  1858. qdf_atomic_init(&pEndpoint->TxProcessCount);
  1859. if (pEndpoint->ul_is_polled) {
  1860. /*
  1861. * Start a cleanup timer to poll for download completion.
  1862. * The download completion should be noticed promptly from
  1863. * other polling calls, but the timer provides a safety net
  1864. * in case other polling calls don't occur as expected.
  1865. */
  1866. htc_send_complete_poll_timer_start(pEndpoint);
  1867. }
  1868. return status;
  1869. }
  1870. #endif /*ATH_11AC_TXCOMPACT */
  1871. qdf_export_symbol(htc_send_data_pkt);
  1872. /*
  1873. * In the adapted HIF layer, qdf_nbuf_t are passed between HIF and HTC,
  1874. * since upper layers expects HTC_PACKET containers we use the completed netbuf
  1875. * and lookup its corresponding HTC packet buffer from a lookup list.
  1876. * This is extra overhead that can be fixed by re-aligning HIF interfaces
  1877. * with HTC.
  1878. *
  1879. */
  1880. static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
  1881. HTC_ENDPOINT *pEndpoint,
  1882. qdf_nbuf_t netbuf)
  1883. {
  1884. HTC_PACKET *pPacket = NULL;
  1885. HTC_PACKET *pFoundPacket = NULL;
  1886. HTC_PACKET_QUEUE lookupQueue;
  1887. INIT_HTC_PACKET_QUEUE(&lookupQueue);
  1888. LOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  1889. LOCK_HTC_TX(target);
  1890. /* mark that HIF has indicated the send complete for another packet */
  1891. pEndpoint->ul_outstanding_cnt--;
  1892. /* Dequeue first packet directly because of in-order completion */
  1893. pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
  1894. if (qdf_unlikely(!pPacket)) {
  1895. UNLOCK_HTC_TX(target);
  1896. UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  1897. return NULL;
  1898. }
  1899. if (netbuf == (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  1900. UNLOCK_HTC_TX(target);
  1901. UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  1902. return pPacket;
  1903. }
  1904. HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
  1905. /*
  1906. * Move TX lookup queue to temp queue because most of packets that are
  1907. * not index 0 are not top 10 packets.
  1908. */
  1909. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
  1910. &pEndpoint->TxLookupQueue);
  1911. ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
  1912. HTC_PACKET, ListLink) {
  1913. if (!pPacket) {
  1914. pFoundPacket = pPacket;
  1915. break;
  1916. }
  1917. /* check for removal */
  1918. if (netbuf ==
  1919. (qdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  1920. /* found it */
  1921. HTC_PACKET_REMOVE(&lookupQueue, pPacket);
  1922. pFoundPacket = pPacket;
  1923. break;
  1924. }
  1925. }
  1926. ITERATE_END;
  1927. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
  1928. &lookupQueue);
  1929. UNLOCK_HTC_TX(target);
  1930. UNLOCK_HTC_EP_TX_LOOKUP(pEndpoint);
  1931. return pFoundPacket;
  1932. }
  1933. /**
  1934. * htc_tx_completion_handler() - htc tx completion handler
  1935. * @Context: pointer to HTC_TARGET structure
  1936. * @netbuf: pointer to netbuf for which completion handler is being called
  1937. * @EpID: end point Id on which the packet was sent
  1938. * @toeplitz_hash_result: toeplitz hash result
  1939. *
  1940. * Return: QDF_STATUS_SUCCESS for success or an appropriate QDF_STATUS error
  1941. */
  1942. QDF_STATUS htc_tx_completion_handler(void *Context,
  1943. qdf_nbuf_t netbuf, unsigned int EpID,
  1944. uint32_t toeplitz_hash_result)
  1945. {
  1946. HTC_TARGET *target = (HTC_TARGET *) Context;
  1947. HTC_ENDPOINT *pEndpoint;
  1948. HTC_PACKET *pPacket;
  1949. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1950. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5, ENDPOINT_4,
  1951. ENDPOINT_2, ENDPOINT_3 };
  1952. int epidIdx;
  1953. uint16_t resourcesThresh[DATA_EP_SIZE]; /* urb resources */
  1954. uint16_t resources;
  1955. uint16_t resourcesMax;
  1956. #endif
  1957. pEndpoint = &target->endpoint[EpID];
  1958. target->TX_comp_cnt++;
  1959. do {
  1960. pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
  1961. if (!pPacket) {
  1962. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1963. ("HTC TX lookup failed!\n"));
  1964. /* may have already been flushed and freed */
  1965. netbuf = NULL;
  1966. break;
  1967. }
  1968. if (pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_AUTO_PM)
  1969. hif_pm_runtime_put(target->hif_dev);
  1970. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
  1971. HTC_PACKET *pPacketTemp;
  1972. HTC_PACKET_QUEUE *pQueueSave =
  1973. (HTC_PACKET_QUEUE *) pPacket->pContext;
  1974. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
  1975. pPacketTemp) {
  1976. pPacket->Status = QDF_STATUS_SUCCESS;
  1977. send_packet_completion(target, pPacketTemp);
  1978. }
  1979. HTC_PACKET_QUEUE_ITERATE_END;
  1980. free_htc_bundle_packet(target, pPacket);
  1981. if (hif_get_bus_type(target->hif_dev) ==
  1982. QDF_BUS_TYPE_USB) {
  1983. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint))
  1984. htc_try_send(target, pEndpoint, NULL);
  1985. }
  1986. return QDF_STATUS_SUCCESS;
  1987. }
  1988. /* will be giving this buffer back to upper layers */
  1989. netbuf = NULL;
  1990. pPacket->Status = QDF_STATUS_SUCCESS;
  1991. send_packet_completion(target, pPacket);
  1992. } while (false);
  1993. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1994. /* note: when using TX credit flow, the re-checking of queues
  1995. * happens when credits flow back from the target. In the non-TX
  1996. * credit case, we recheck after the packet completes
  1997. */
  1998. if ((qdf_atomic_read(&pEndpoint->TxProcessCount) == 0) ||
  1999. (!pEndpoint->async_update)) {
  2000. htc_try_send(target, pEndpoint, NULL);
  2001. }
  2002. }
  2003. return QDF_STATUS_SUCCESS;
  2004. }
  2005. #ifdef WLAN_FEATURE_FASTPATH
  2006. /**
  2007. * htc_ctrl_msg_cmpl(): checks for tx completion for the endpoint specified
  2008. * @HTC_HANDLE : pointer to the htc target context
  2009. * @htc_ep_id : end point id
  2010. *
  2011. * checks HTC tx completion
  2012. *
  2013. * Return: none
  2014. */
  2015. void htc_ctrl_msg_cmpl(HTC_HANDLE htc_pdev, HTC_ENDPOINT_ID htc_ep_id)
  2016. {
  2017. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_pdev);
  2018. HTC_ENDPOINT *pendpoint = &target->endpoint[htc_ep_id];
  2019. htc_send_complete_check(pendpoint, 1);
  2020. }
  2021. qdf_export_symbol(htc_ctrl_msg_cmpl);
  2022. #endif
  2023. /* callback when TX resources become available */
  2024. void htc_tx_resource_avail_handler(void *context, uint8_t pipeID)
  2025. {
  2026. int i;
  2027. HTC_TARGET *target = (HTC_TARGET *) context;
  2028. HTC_ENDPOINT *pEndpoint = NULL;
  2029. for (i = 0; i < ENDPOINT_MAX; i++) {
  2030. pEndpoint = &target->endpoint[i];
  2031. if (pEndpoint->service_id != 0) {
  2032. if (pEndpoint->UL_PipeID == pipeID)
  2033. break;
  2034. }
  2035. }
  2036. if (i >= ENDPOINT_MAX) {
  2037. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  2038. ("Invalid pipe indicated for TX resource avail : %d!\n",
  2039. pipeID));
  2040. return;
  2041. }
  2042. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  2043. ("HIF indicated more resources for pipe:%d\n",
  2044. pipeID));
  2045. htc_try_send(target, pEndpoint, NULL);
  2046. }
  2047. #ifdef FEATURE_RUNTIME_PM
  2048. /**
  2049. * htc_kick_queues(): resumes tx transactions of suspended endpoints
  2050. * @context: pointer to the htc target context
  2051. *
  2052. * Iterates through the enpoints and provides a context to empty queues
  2053. * int the hif layer when they are stalled due to runtime suspend.
  2054. *
  2055. * Return: none
  2056. */
  2057. void htc_kick_queues(void *context)
  2058. {
  2059. int i;
  2060. HTC_TARGET *target = (HTC_TARGET *)context;
  2061. HTC_ENDPOINT *endpoint = NULL;
  2062. for (i = 0; i < ENDPOINT_MAX; i++) {
  2063. endpoint = &target->endpoint[i];
  2064. if (endpoint->service_id == 0)
  2065. continue;
  2066. if (endpoint->EpCallBacks.ep_resume_tx_queue)
  2067. endpoint->EpCallBacks.ep_resume_tx_queue(
  2068. endpoint->EpCallBacks.pContext);
  2069. htc_try_send(target, endpoint, NULL);
  2070. }
  2071. hif_fastpath_resume(target->hif_dev);
  2072. }
  2073. #endif
  2074. /* flush endpoint TX queue */
  2075. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  2076. HTC_TX_TAG Tag)
  2077. {
  2078. HTC_PACKET *pPacket;
  2079. LOCK_HTC_TX(target);
  2080. while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  2081. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  2082. if (pPacket) {
  2083. /* let the sender know the packet was not delivered */
  2084. pPacket->Status = QDF_STATUS_E_CANCELED;
  2085. send_packet_completion(target, pPacket);
  2086. }
  2087. }
  2088. UNLOCK_HTC_TX(target);
  2089. }
  2090. /* flush pending entries in endpoint TX Lookup queue */
  2091. void htc_flush_endpoint_txlookupQ(HTC_TARGET *target,
  2092. HTC_ENDPOINT_ID endpoint_id,
  2093. bool call_ep_callback)
  2094. {
  2095. HTC_PACKET *packet;
  2096. HTC_ENDPOINT *endpoint;
  2097. endpoint = &target->endpoint[endpoint_id];
  2098. if (!endpoint && endpoint->service_id == 0)
  2099. return;
  2100. LOCK_HTC_TX(target);
  2101. while (HTC_PACKET_QUEUE_DEPTH(&endpoint->TxLookupQueue)) {
  2102. packet = htc_packet_dequeue(&endpoint->TxLookupQueue);
  2103. if (packet) {
  2104. if (call_ep_callback == true) {
  2105. packet->Status = QDF_STATUS_E_CANCELED;
  2106. send_packet_completion(target, packet);
  2107. } else {
  2108. qdf_mem_free(packet);
  2109. }
  2110. }
  2111. }
  2112. UNLOCK_HTC_TX(target);
  2113. }
  2114. /* HTC API to flush an endpoint's TX queue*/
  2115. void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
  2116. HTC_TX_TAG Tag)
  2117. {
  2118. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  2119. HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
  2120. if (pEndpoint->service_id == 0) {
  2121. AR_DEBUG_ASSERT(false);
  2122. /* not in use.. */
  2123. return;
  2124. }
  2125. htc_flush_endpoint_tx(target, pEndpoint, Tag);
  2126. }
  2127. /* HTC API to indicate activity to the credit distribution function */
  2128. void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
  2129. HTC_ENDPOINT_ID Endpoint, bool Active)
  2130. {
  2131. /* TODO */
  2132. }
  2133. bool htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
  2134. {
  2135. return true;
  2136. }
  2137. void htc_set_nodrop_pkt(HTC_HANDLE HTCHandle, A_BOOL isNodropPkt)
  2138. {
  2139. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  2140. target->is_nodrop_pkt = isNodropPkt;
  2141. }
  2142. void htc_enable_hdr_length_check(HTC_HANDLE htc_hdl, bool htc_hdr_length_check)
  2143. {
  2144. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_hdl);
  2145. target->htc_hdr_length_check = htc_hdr_length_check;
  2146. }
  2147. /**
  2148. * htc_process_credit_rpt() - process credit report, call distribution function
  2149. * @target: pointer to HTC_TARGET
  2150. * @pRpt: pointer to HTC_CREDIT_REPORT
  2151. * @NumEntries: number of entries in credit report
  2152. * @FromEndpoint: endpoint for which credit report is received
  2153. *
  2154. * Return: A_OK for success or an appropriate A_STATUS error
  2155. */
  2156. void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
  2157. int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
  2158. {
  2159. int i;
  2160. HTC_ENDPOINT *pEndpoint;
  2161. int totalCredits = 0;
  2162. uint8_t rpt_credits, rpt_ep_id;
  2163. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  2164. ("+htc_process_credit_rpt, Credit Report Entries:%d\n",
  2165. NumEntries));
  2166. /* lock out TX while we update credits */
  2167. LOCK_HTC_TX(target);
  2168. for (i = 0; i < NumEntries; i++, pRpt++) {
  2169. rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
  2170. if (rpt_ep_id >= ENDPOINT_MAX) {
  2171. AR_DEBUG_ASSERT(false);
  2172. break;
  2173. }
  2174. rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
  2175. pEndpoint = &target->endpoint[rpt_ep_id];
  2176. #if DEBUG_CREDIT
  2177. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  2178. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  2179. (" <HTC> Increase EP%d %d + %d = %d credits\n",
  2180. rpt_ep_id, pEndpoint->TxCredits,
  2181. rpt_credits,
  2182. pEndpoint->TxCredits + rpt_credits));
  2183. }
  2184. #endif
  2185. #ifdef HTC_EP_STAT_PROFILING
  2186. INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
  2187. INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
  2188. if (FromEndpoint == rpt_ep_id) {
  2189. /* this credit report arrived on the same endpoint
  2190. * indicating it arrived in an RX packet
  2191. */
  2192. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
  2193. rpt_credits);
  2194. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
  2195. } else if (FromEndpoint == ENDPOINT_0) {
  2196. /* this credit arrived on endpoint 0 as a NULL msg */
  2197. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
  2198. rpt_credits);
  2199. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
  2200. } else {
  2201. /* arrived on another endpoint */
  2202. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
  2203. rpt_credits);
  2204. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
  2205. }
  2206. #endif
  2207. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  2208. htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
  2209. pEndpoint->TxCredits + rpt_credits,
  2210. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  2211. TxQueue));
  2212. }
  2213. pEndpoint->TxCredits += rpt_credits;
  2214. if (pEndpoint->TxCredits
  2215. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  2216. UNLOCK_HTC_TX(target);
  2217. #ifdef ATH_11AC_TXCOMPACT
  2218. htc_try_send(target, pEndpoint, NULL);
  2219. #else
  2220. if (pEndpoint->service_id == HTT_DATA_MSG_SVC)
  2221. htc_send_data_pkt((HTC_HANDLE)target, NULL, 0);
  2222. else
  2223. htc_try_send(target, pEndpoint, NULL);
  2224. #endif
  2225. LOCK_HTC_TX(target);
  2226. }
  2227. totalCredits += rpt_credits;
  2228. }
  2229. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  2230. (" Report indicated %d credits to distribute\n",
  2231. totalCredits));
  2232. UNLOCK_HTC_TX(target);
  2233. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt\n"));
  2234. }
  2235. /* function to fetch stats from htc layer*/
  2236. struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
  2237. {
  2238. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  2239. return &(target->htc_pkt_stats);
  2240. }