htc_send.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  1. /*
  2. * Copyright (c) 2013-2016 The Linux Foundation. All rights reserved.
  3. *
  4. * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
  5. *
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for
  8. * any purpose with or without fee is hereby granted, provided that the
  9. * above copyright notice and this permission notice appear in all
  10. * copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  13. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  15. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  16. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  17. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  18. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  19. * PERFORMANCE OF THIS SOFTWARE.
  20. */
  21. /*
  22. * This file was originally distributed by Qualcomm Atheros, Inc.
  23. * under proprietary terms before Copyright ownership was assigned
  24. * to the Linux Foundation.
  25. */
  26. #include "htc_debug.h"
  27. #include "htc_internal.h"
  28. #include <cdf_nbuf.h> /* cdf_nbuf_t */
  29. #include <qdf_mem.h> /* qdf_mem_malloc */
  30. #include "epping_main.h"
  31. /* #define USB_HIF_SINGLE_PIPE_DATA_SCHED */
  32. /* #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED */
  33. #define DATA_EP_SIZE 4
  34. /* #endif */
  35. #define HTC_DATA_RESOURCE_THRS 256
  36. #define HTC_DATA_MINDESC_PERPACKET 2
  37. typedef enum _HTC_SEND_QUEUE_RESULT {
  38. HTC_SEND_QUEUE_OK = 0, /* packet was queued */
  39. HTC_SEND_QUEUE_DROP = 1, /* this packet should be dropped */
  40. } HTC_SEND_QUEUE_RESULT;
  41. #ifndef DEBUG_CREDIT
  42. #define DEBUG_CREDIT 0
  43. #endif
  44. #if DEBUG_CREDIT
  45. /* bit mask to enable debug certain endpoint */
  46. static unsigned ep_debug_mask =
  47. (1 << ENDPOINT_0) | (1 << ENDPOINT_1) | (1 << ENDPOINT_2);
  48. #endif
  49. /* HTC Control Path Credit History */
  50. A_UINT32 g_htc_credit_history_idx = 0;
  51. HTC_CREDIT_HISTORY htc_credit_history_buffer[HTC_CREDIT_HISTORY_MAX];
  52. /**
  53. * htc_credit_record() - records tx que state & credit transactions
  54. * @type: type of echange can be HTC_REQUEST_CREDIT
  55. * or HTC_PROCESS_CREDIT_REPORT
  56. * @tx_credits: current number of tx_credits
  57. * @htc_tx_queue_depth: current hct tx queue depth
  58. *
  59. * This function records the credits and pending commands whenever a command is
  60. * sent or credits are returned. Call this after the credits have been updated
  61. * according to the transaction. Call this before dequeing commands.
  62. *
  63. * Consider making this function accept an HTC_ENDPOINT and find the current
  64. * credits and queue depth itself.
  65. *
  66. * Consider moving the LOCK_HTC_CREDIT(target); logic into this function as well.
  67. */
  68. void htc_credit_record(htc_credit_exchange_type type, uint32_t tx_credit,
  69. uint32_t htc_tx_queue_depth) {
  70. if (HTC_CREDIT_HISTORY_MAX <= g_htc_credit_history_idx)
  71. g_htc_credit_history_idx = 0;
  72. htc_credit_history_buffer[g_htc_credit_history_idx].type = type;
  73. htc_credit_history_buffer[g_htc_credit_history_idx].time =
  74. qdf_get_log_timestamp();
  75. htc_credit_history_buffer[g_htc_credit_history_idx].tx_credit =
  76. tx_credit;
  77. htc_credit_history_buffer[g_htc_credit_history_idx].htc_tx_queue_depth =
  78. htc_tx_queue_depth;
  79. g_htc_credit_history_idx++;
  80. #ifdef QCA_WIFI_3_0_EMU
  81. if (type == HTC_REQUEST_CREDIT)
  82. printk("\nrequest_credits-> current_credit %d, pending commands %d\n",
  83. tx_credit, htc_tx_queue_depth);
  84. else if (type == HTC_PROCESS_CREDIT_REPORT)
  85. printk("\ncredit_report<- current_credit %d, pending commands %d\n",
  86. tx_credit, htc_tx_queue_depth);
  87. #endif
  88. }
  89. void htc_dump_counter_info(HTC_HANDLE HTCHandle)
  90. {
  91. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  92. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  93. ("\n%s: ce_send_cnt = %d, TX_comp_cnt = %d\n",
  94. __func__, target->ce_send_cnt, target->TX_comp_cnt));
  95. }
  96. void htc_get_control_endpoint_tx_host_credits(HTC_HANDLE HTCHandle, int *credits)
  97. {
  98. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  99. HTC_ENDPOINT *pEndpoint;
  100. int i;
  101. if (!credits || !target) {
  102. AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: invalid args", __func__));
  103. return;
  104. }
  105. *credits = 0;
  106. LOCK_HTC_TX(target);
  107. for (i = 0; i < ENDPOINT_MAX; i++) {
  108. pEndpoint = &target->endpoint[i];
  109. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  110. *credits = pEndpoint->TxCredits;
  111. break;
  112. }
  113. }
  114. UNLOCK_HTC_TX(target);
  115. }
  116. static inline void restore_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
  117. {
  118. if (pPacket->PktInfo.AsTx.Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) {
  119. cdf_nbuf_t netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  120. cdf_nbuf_unmap(target->osdev, netbuf, QDF_DMA_TO_DEVICE);
  121. cdf_nbuf_pull_head(netbuf, sizeof(HTC_FRAME_HDR));
  122. pPacket->PktInfo.AsTx.Flags &= ~HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  123. }
  124. }
  125. static void do_send_completion(HTC_ENDPOINT *pEndpoint,
  126. HTC_PACKET_QUEUE *pQueueToIndicate)
  127. {
  128. do {
  129. if (HTC_QUEUE_EMPTY(pQueueToIndicate)) {
  130. /* nothing to indicate */
  131. break;
  132. }
  133. if (pEndpoint->EpCallBacks.EpTxCompleteMultiple != NULL) {
  134. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  135. (" HTC calling ep %d, send complete multiple callback (%d pkts) \n",
  136. pEndpoint->Id,
  137. HTC_PACKET_QUEUE_DEPTH
  138. (pQueueToIndicate)));
  139. /* a multiple send complete handler is being used, pass the queue to the handler */
  140. pEndpoint->EpCallBacks.EpTxCompleteMultiple(pEndpoint->
  141. EpCallBacks.
  142. pContext,
  143. pQueueToIndicate);
  144. /* all packets are now owned by the callback, reset queue to be safe */
  145. INIT_HTC_PACKET_QUEUE(pQueueToIndicate);
  146. } else {
  147. HTC_PACKET *pPacket;
  148. /* using legacy EpTxComplete */
  149. do {
  150. pPacket = htc_packet_dequeue(pQueueToIndicate);
  151. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  152. (" HTC calling ep %d send complete callback on packet %p \n",
  153. pEndpoint->Id, pPacket));
  154. pEndpoint->EpCallBacks.EpTxComplete(pEndpoint->
  155. EpCallBacks.
  156. pContext,
  157. pPacket);
  158. } while (!HTC_QUEUE_EMPTY(pQueueToIndicate));
  159. }
  160. } while (false);
  161. }
  162. static void send_packet_completion(HTC_TARGET *target, HTC_PACKET *pPacket)
  163. {
  164. HTC_ENDPOINT *pEndpoint = &target->endpoint[pPacket->Endpoint];
  165. HTC_PACKET_QUEUE container;
  166. restore_tx_packet(target, pPacket);
  167. INIT_HTC_PACKET_QUEUE_AND_ADD(&container, pPacket);
  168. /* do completion */
  169. do_send_completion(pEndpoint, &container);
  170. }
  171. void htc_send_complete_check_cleanup(void *context)
  172. {
  173. HTC_ENDPOINT *pEndpoint = (HTC_ENDPOINT *) context;
  174. htc_send_complete_check(pEndpoint, 1);
  175. }
  176. HTC_PACKET *allocate_htc_bundle_packet(HTC_TARGET *target)
  177. {
  178. HTC_PACKET *pPacket;
  179. HTC_PACKET_QUEUE *pQueueSave;
  180. cdf_nbuf_t netbuf;
  181. LOCK_HTC_TX(target);
  182. if (NULL == target->pBundleFreeList) {
  183. UNLOCK_HTC_TX(target);
  184. netbuf = cdf_nbuf_alloc(NULL,
  185. target->MaxMsgsPerHTCBundle *
  186. target->TargetCreditSize, 0, 4, false);
  187. AR_DEBUG_ASSERT(netbuf);
  188. if (!netbuf) {
  189. return NULL;
  190. }
  191. pPacket = qdf_mem_malloc(sizeof(HTC_PACKET));
  192. AR_DEBUG_ASSERT(pPacket);
  193. if (!pPacket) {
  194. cdf_nbuf_free(netbuf);
  195. return NULL;
  196. }
  197. pQueueSave = qdf_mem_malloc(sizeof(HTC_PACKET_QUEUE));
  198. AR_DEBUG_ASSERT(pQueueSave);
  199. if (!pQueueSave) {
  200. cdf_nbuf_free(netbuf);
  201. qdf_mem_free(pPacket);
  202. return NULL;
  203. }
  204. INIT_HTC_PACKET_QUEUE(pQueueSave);
  205. pPacket->pContext = pQueueSave;
  206. SET_HTC_PACKET_NET_BUF_CONTEXT(pPacket, netbuf);
  207. pPacket->pBuffer = cdf_nbuf_data(netbuf);
  208. pPacket->BufferLength = cdf_nbuf_len(netbuf);
  209. /* store the original head room so that we can restore this when we "free" the packet */
  210. /* free packet puts the packet back on the free list */
  211. pPacket->netbufOrigHeadRoom = cdf_nbuf_headroom(netbuf);
  212. return pPacket;
  213. }
  214. /* already done malloc - restore from free list */
  215. pPacket = target->pBundleFreeList;
  216. AR_DEBUG_ASSERT(pPacket);
  217. if (!pPacket) {
  218. UNLOCK_HTC_TX(target);
  219. return NULL;
  220. }
  221. target->pBundleFreeList = (HTC_PACKET *) pPacket->ListLink.pNext;
  222. UNLOCK_HTC_TX(target);
  223. pPacket->ListLink.pNext = NULL;
  224. return pPacket;
  225. }
  226. void free_htc_bundle_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
  227. {
  228. A_UINT32 curentHeadRoom;
  229. cdf_nbuf_t netbuf;
  230. HTC_PACKET_QUEUE *pQueueSave;
  231. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  232. AR_DEBUG_ASSERT(netbuf);
  233. if (!netbuf) {
  234. AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("\n%s: Invalid netbuf in HTC "
  235. "Packet\n", __func__));
  236. return;
  237. }
  238. /* HIF adds data to the headroom section of the nbuf, restore the original */
  239. /* size. If this is not done, headroom keeps shrinking with every HIF send */
  240. /* and eventually HIF ends up doing another malloc big enough to store the */
  241. /* data + its header */
  242. curentHeadRoom = cdf_nbuf_headroom(netbuf);
  243. cdf_nbuf_pull_head(netbuf,
  244. pPacket->netbufOrigHeadRoom - curentHeadRoom);
  245. cdf_nbuf_trim_tail(netbuf, cdf_nbuf_len(netbuf));
  246. /* restore the pBuffer pointer. HIF changes this */
  247. pPacket->pBuffer = cdf_nbuf_data(netbuf);
  248. pPacket->BufferLength = cdf_nbuf_len(netbuf);
  249. /* restore queue */
  250. pQueueSave = (HTC_PACKET_QUEUE *) pPacket->pContext;
  251. AR_DEBUG_ASSERT(pQueueSave);
  252. INIT_HTC_PACKET_QUEUE(pQueueSave);
  253. LOCK_HTC_TX(target);
  254. if (target->pBundleFreeList == NULL) {
  255. target->pBundleFreeList = pPacket;
  256. pPacket->ListLink.pNext = NULL;
  257. } else {
  258. pPacket->ListLink.pNext = (DL_LIST *) target->pBundleFreeList;
  259. target->pBundleFreeList = pPacket;
  260. }
  261. UNLOCK_HTC_TX(target);
  262. }
  263. #if defined(HIF_USB) || defined(HIF_SDIO)
  264. #ifdef ENABLE_BUNDLE_TX
  265. static A_STATUS htc_send_bundled_netbuf(HTC_TARGET *target,
  266. HTC_ENDPOINT *pEndpoint,
  267. unsigned char *pBundleBuffer,
  268. HTC_PACKET *pPacketTx)
  269. {
  270. qdf_size_t data_len;
  271. A_STATUS status;
  272. cdf_nbuf_t bundleBuf;
  273. uint32_t data_attr = 0;
  274. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  275. data_len = pBundleBuffer - cdf_nbuf_data(bundleBuf);
  276. cdf_nbuf_put_tail(bundleBuf, data_len);
  277. SET_HTC_PACKET_INFO_TX(pPacketTx,
  278. target,
  279. pBundleBuffer,
  280. data_len,
  281. pEndpoint->Id, HTC_TX_PACKET_TAG_BUNDLED);
  282. LOCK_HTC_TX(target);
  283. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacketTx);
  284. UNLOCK_HTC_TX(target);
  285. #if DEBUG_BUNDLE
  286. qdf_print(" Send bundle EP%d buffer size:0x%x, total:0x%x, count:%d.\n",
  287. pEndpoint->Id,
  288. pEndpoint->TxCreditSize,
  289. data_len, data_len / pEndpoint->TxCreditSize);
  290. #endif
  291. status = hif_send_head(target->hif_dev,
  292. pEndpoint->UL_PipeID,
  293. pEndpoint->Id, data_len, bundleBuf, data_attr);
  294. if (status != A_OK) {
  295. qdf_print("%s:hif_send_head failed(len=%d).\n", __FUNCTION__,
  296. data_len);
  297. }
  298. return status;
  299. }
  300. static void htc_issue_packets_bundle(HTC_TARGET *target,
  301. HTC_ENDPOINT *pEndpoint,
  302. HTC_PACKET_QUEUE *pPktQueue)
  303. {
  304. int i, frag_count, nbytes;
  305. cdf_nbuf_t netbuf, bundleBuf;
  306. unsigned char *pBundleBuffer = NULL;
  307. HTC_PACKET *pPacket = NULL, *pPacketTx = NULL;
  308. HTC_FRAME_HDR *pHtcHdr;
  309. int creditPad, creditRemainder, transferLength, bundlesSpaceRemaining =
  310. 0;
  311. HTC_PACKET_QUEUE *pQueueSave = NULL;
  312. bundlesSpaceRemaining =
  313. target->MaxMsgsPerHTCBundle * pEndpoint->TxCreditSize;
  314. pPacketTx = allocate_htc_bundle_packet(target);
  315. if (!pPacketTx) {
  316. /* good time to panic */
  317. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  318. ("allocate_htc_bundle_packet failed \n"));
  319. AR_DEBUG_ASSERT(false);
  320. return;
  321. }
  322. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  323. pBundleBuffer = cdf_nbuf_data(bundleBuf);
  324. pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
  325. while (1) {
  326. pPacket = htc_packet_dequeue(pPktQueue);
  327. if (pPacket == NULL) {
  328. break;
  329. }
  330. creditPad = 0;
  331. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  332. creditRemainder = transferLength % pEndpoint->TxCreditSize;
  333. if (creditRemainder != 0) {
  334. if (transferLength < pEndpoint->TxCreditSize) {
  335. creditPad =
  336. pEndpoint->TxCreditSize - transferLength;
  337. } else {
  338. creditPad = creditRemainder;
  339. }
  340. transferLength += creditPad;
  341. }
  342. if (bundlesSpaceRemaining < transferLength) {
  343. /* send out previous buffer */
  344. htc_send_bundled_netbuf(target, pEndpoint, pBundleBuffer,
  345. pPacketTx);
  346. if (HTC_PACKET_QUEUE_DEPTH(pPktQueue) <
  347. HTC_MIN_MSG_PER_BUNDLE) {
  348. return;
  349. }
  350. bundlesSpaceRemaining =
  351. target->MaxMsgsPerHTCBundle *
  352. pEndpoint->TxCreditSize;
  353. pPacketTx = allocate_htc_bundle_packet(target);
  354. if (!pPacketTx) {
  355. /* good time to panic */
  356. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  357. ("allocate_htc_bundle_packet failed \n"));
  358. AR_DEBUG_ASSERT(false);
  359. return;
  360. }
  361. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  362. pBundleBuffer = cdf_nbuf_data(bundleBuf);
  363. pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
  364. }
  365. bundlesSpaceRemaining -= transferLength;
  366. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  367. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  368. HTC_WRITE32(pHtcHdr,
  369. SM(pPacket->ActualLength,
  370. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->PktInfo.
  371. AsTx.
  372. SendFlags |
  373. HTC_FLAGS_SEND_BUNDLE,
  374. HTC_FRAME_HDR_FLAGS)
  375. | SM(pPacket->Endpoint, HTC_FRAME_HDR_ENDPOINTID));
  376. HTC_WRITE32((A_UINT32 *) pHtcHdr + 1,
  377. SM(pPacket->PktInfo.AsTx.SeqNo,
  378. HTC_FRAME_HDR_CONTROLBYTES1) | SM(creditPad,
  379. HTC_FRAME_HDR_RESERVED));
  380. pHtcHdr->reserved = creditPad;
  381. frag_count = cdf_nbuf_get_num_frags(netbuf);
  382. nbytes = pPacket->ActualLength + HTC_HDR_LENGTH;
  383. for (i = 0; i < frag_count && nbytes > 0; i++) {
  384. int frag_len = cdf_nbuf_get_frag_len(netbuf, i);
  385. unsigned char *frag_addr =
  386. cdf_nbuf_get_frag_vaddr(netbuf, i);
  387. if (frag_len > nbytes) {
  388. frag_len = nbytes;
  389. }
  390. A_MEMCPY(pBundleBuffer, frag_addr, frag_len);
  391. nbytes -= frag_len;
  392. pBundleBuffer += frag_len;
  393. }
  394. HTC_PACKET_ENQUEUE(pQueueSave, pPacket);
  395. pBundleBuffer += creditPad;
  396. }
  397. if (pBundleBuffer != cdf_nbuf_data(bundleBuf)) {
  398. /* send out remaining buffer */
  399. htc_send_bundled_netbuf(target, pEndpoint, pBundleBuffer,
  400. pPacketTx);
  401. } else {
  402. free_htc_bundle_packet(target, pPacketTx);
  403. }
  404. }
  405. #endif /* ENABLE_BUNDLE_TX */
  406. #endif
  407. static A_STATUS htc_issue_packets(HTC_TARGET *target,
  408. HTC_ENDPOINT *pEndpoint,
  409. HTC_PACKET_QUEUE *pPktQueue)
  410. {
  411. A_STATUS status = A_OK;
  412. cdf_nbuf_t netbuf;
  413. HTC_PACKET *pPacket = NULL;
  414. uint16_t payloadLen;
  415. HTC_FRAME_HDR *pHtcHdr;
  416. uint32_t data_attr = 0;
  417. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  418. ("+htc_issue_packets: Queue: %p, Pkts %d \n", pPktQueue,
  419. HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
  420. while (true) {
  421. #if defined(HIF_USB) || defined(HIF_SDIO)
  422. #ifdef ENABLE_BUNDLE_TX
  423. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint) &&
  424. HTC_ENABLE_BUNDLE(target) &&
  425. HTC_PACKET_QUEUE_DEPTH(pPktQueue) >=
  426. HTC_MIN_MSG_PER_BUNDLE) {
  427. htc_issue_packets_bundle(target, pEndpoint, pPktQueue);
  428. }
  429. #endif
  430. #endif
  431. /* if not bundling or there was a packet that could not be placed in a bundle,
  432. * and send it by normal way
  433. */
  434. pPacket = htc_packet_dequeue(pPktQueue);
  435. if (NULL == pPacket) {
  436. /* local queue is fully drained */
  437. break;
  438. }
  439. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  440. AR_DEBUG_ASSERT(netbuf);
  441. /* Non-credit enabled endpoints have been mapped and setup by now,
  442. * so no need to revisit the HTC headers
  443. */
  444. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  445. payloadLen = pPacket->ActualLength;
  446. /* setup HTC frame header */
  447. pHtcHdr =
  448. (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf,
  449. 0);
  450. AR_DEBUG_ASSERT(pHtcHdr);
  451. HTC_WRITE32(pHtcHdr,
  452. SM(payloadLen,
  453. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->
  454. PktInfo.
  455. AsTx.
  456. SendFlags,
  457. HTC_FRAME_HDR_FLAGS)
  458. | SM(pPacket->Endpoint,
  459. HTC_FRAME_HDR_ENDPOINTID));
  460. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  461. SM(pPacket->PktInfo.AsTx.SeqNo,
  462. HTC_FRAME_HDR_CONTROLBYTES1));
  463. /*
  464. * Now that the HTC frame header has been added, the netbuf can be
  465. * mapped. This only applies to non-data frames, since data frames
  466. * were already mapped as they entered into the driver.
  467. * Check the "FIXUP_NETBUF" flag to see whether this is a data netbuf
  468. * that is already mapped, or a non-data netbuf that needs to be
  469. * mapped.
  470. */
  471. if (pPacket->PktInfo.AsTx.
  472. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) {
  473. cdf_nbuf_map(target->osdev,
  474. GET_HTC_PACKET_NET_BUF_CONTEXT
  475. (pPacket), QDF_DMA_TO_DEVICE);
  476. }
  477. }
  478. LOCK_HTC_TX(target);
  479. /* store in look up queue to match completions */
  480. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  481. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  482. pEndpoint->ul_outstanding_cnt++;
  483. UNLOCK_HTC_TX(target);
  484. hif_send_complete_check(target->hif_dev, pEndpoint->UL_PipeID, false);
  485. status = hif_send_head(target->hif_dev,
  486. pEndpoint->UL_PipeID, pEndpoint->Id,
  487. HTC_HDR_LENGTH + pPacket->ActualLength,
  488. netbuf, data_attr);
  489. #if DEBUG_BUNDLE
  490. qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.\n",
  491. pEndpoint->Id,
  492. pEndpoint->TxCreditSize,
  493. HTC_HDR_LENGTH + pPacket->ActualLength);
  494. #endif
  495. target->ce_send_cnt++;
  496. if (qdf_unlikely(A_FAILED(status))) {
  497. if (status != A_NO_RESOURCE) {
  498. /* TODO : if more than 1 endpoint maps to the same PipeID it is possible
  499. * to run out of resources in the HIF layer. Don't emit the error */
  500. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  501. ("hif_send Failed status:%d \n",
  502. status));
  503. }
  504. LOCK_HTC_TX(target);
  505. target->ce_send_cnt--;
  506. pEndpoint->ul_outstanding_cnt--;
  507. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  508. /* reclaim credits */
  509. #if defined(HIF_USB)
  510. if (pEndpoint->Id >= ENDPOINT_2
  511. && pEndpoint->Id <= ENDPOINT_5)
  512. target->avail_tx_credits +=
  513. pPacket->PktInfo.AsTx.CreditsUsed;
  514. else
  515. pEndpoint->TxCredits +=
  516. pPacket->PktInfo.AsTx.CreditsUsed;
  517. #else
  518. pEndpoint->TxCredits +=
  519. pPacket->PktInfo.AsTx.CreditsUsed;
  520. #endif
  521. /* put it back into the callers queue */
  522. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  523. UNLOCK_HTC_TX(target);
  524. break;
  525. }
  526. /*
  527. * For HTT messages without a response from fw,
  528. * do the runtime put here.
  529. * otherwise runtime put will be done when the fw response comes
  530. */
  531. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_RUNTIME_PUT)
  532. hif_pm_runtime_put(target->hif_dev);
  533. }
  534. if (qdf_unlikely(A_FAILED(status))) {
  535. #if defined(HIF_USB)
  536. if (pEndpoint->Id >= ENDPOINT_2 && pEndpoint->Id <= ENDPOINT_5)
  537. target->avail_tx_credits +=
  538. pPacket->PktInfo.AsTx.CreditsUsed;
  539. else
  540. pEndpoint->TxCredits +=
  541. pPacket->PktInfo.AsTx.CreditsUsed;
  542. #endif
  543. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  544. ("htc_issue_packets, failed pkt:0x%p status:%d",
  545. pPacket, status));
  546. }
  547. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_issue_packets \n"));
  548. return status;
  549. }
  550. #ifdef FEATURE_RUNTIME_PM
  551. /**
  552. * extract_htc_pm_packtes(): move pm packets from endpoint into queue
  553. * @endpoint: which enpoint to extract packets from
  554. * @queue: a queue to store extracted packets in.
  555. *
  556. * remove pm packets from the endpoint's tx queue.
  557. * queue them into a queue
  558. */
  559. static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
  560. HTC_PACKET_QUEUE *queue)
  561. {
  562. HTC_PACKET *packet;
  563. /* only WMI endpoint has power management packets */
  564. if (endpoint->service_id != WMI_CONTROL_SVC)
  565. return;
  566. ITERATE_OVER_LIST_ALLOW_REMOVE(&endpoint->TxQueue.QueueHead, packet,
  567. HTC_PACKET, ListLink) {
  568. if (packet->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_AUTO_PM) {
  569. HTC_PACKET_REMOVE(&endpoint->TxQueue, packet);
  570. HTC_PACKET_ENQUEUE(queue, packet);
  571. }
  572. } ITERATE_END
  573. }
  574. /**
  575. * queue_htc_pm_packets(): queue pm packets with priority
  576. * @endpoint: enpoint to queue packets to
  577. * @queue: queue of pm packets to enque
  578. *
  579. * suspend resume packets get special treatment & priority.
  580. * need to queue them at the front of the queue.
  581. */
  582. static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
  583. HTC_PACKET_QUEUE *queue)
  584. {
  585. if (endpoint->service_id != WMI_CONTROL_SVC)
  586. return;
  587. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&endpoint->TxQueue, queue);
  588. }
  589. #else
  590. static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
  591. HTC_PACKET_QUEUE *queue)
  592. {}
  593. static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
  594. HTC_PACKET_QUEUE *queue)
  595. {}
  596. #endif
  597. /* get HTC send packets from the TX queue on an endpoint, based on available credits */
  598. void get_htc_send_packets_credit_based(HTC_TARGET *target,
  599. HTC_ENDPOINT *pEndpoint,
  600. HTC_PACKET_QUEUE *pQueue)
  601. {
  602. int creditsRequired;
  603. int remainder;
  604. A_UINT8 sendFlags;
  605. HTC_PACKET *pPacket;
  606. unsigned int transferLength;
  607. HTC_PACKET_QUEUE *tx_queue;
  608. HTC_PACKET_QUEUE pm_queue;
  609. bool do_pm_get = false;
  610. /****** NOTE : the TX lock is held when this function is called *****************/
  611. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+get_htc_send_packets_credit_based\n"));
  612. INIT_HTC_PACKET_QUEUE(&pm_queue);
  613. extract_htc_pm_packets(pEndpoint, &pm_queue);
  614. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  615. tx_queue = &pEndpoint->TxQueue;
  616. do_pm_get = true;
  617. } else {
  618. tx_queue = &pm_queue;
  619. }
  620. /* loop until we can grab as many packets out of the queue as we can */
  621. while (true) {
  622. if (do_pm_get && hif_pm_runtime_get(target->hif_dev)) {
  623. /* bus suspended, runtime resume issued */
  624. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  625. break;
  626. }
  627. sendFlags = 0;
  628. /* get packet at head, but don't remove it */
  629. pPacket = htc_get_pkt_at_head(tx_queue);
  630. if (pPacket == NULL) {
  631. if (do_pm_get)
  632. hif_pm_runtime_put(target->hif_dev);
  633. break;
  634. }
  635. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  636. (" Got head packet:%p , Queue Depth: %d\n",
  637. pPacket,
  638. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  639. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  640. if (transferLength <= pEndpoint->TxCreditSize) {
  641. creditsRequired = 1;
  642. } else {
  643. /* figure out how many credits this message requires */
  644. creditsRequired =
  645. transferLength / pEndpoint->TxCreditSize;
  646. remainder = transferLength % pEndpoint->TxCreditSize;
  647. if (remainder) {
  648. creditsRequired++;
  649. }
  650. }
  651. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  652. (" Credits Required:%d Got:%d\n",
  653. creditsRequired, pEndpoint->TxCredits));
  654. if (pEndpoint->Id == ENDPOINT_0) {
  655. /* endpoint 0 is special, it always has a credit and does not require credit based
  656. * flow control */
  657. creditsRequired = 0;
  658. #if defined(HIF_USB)
  659. } else if (pEndpoint->Id >= ENDPOINT_2
  660. && pEndpoint->Id <= ENDPOINT_5) {
  661. if (target->avail_tx_credits < creditsRequired)
  662. break;
  663. target->avail_tx_credits -= creditsRequired;
  664. if (target->avail_tx_credits < 9) {
  665. /* tell the target we need credits ASAP! */
  666. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  667. INC_HTC_EP_STAT(pEndpoint,
  668. TxCreditLowIndications, 1);
  669. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  670. (" Host Needs Credits \n"));
  671. }
  672. #endif
  673. } else {
  674. if (pEndpoint->TxCredits < creditsRequired) {
  675. #if DEBUG_CREDIT
  676. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  677. (" EP%d, No Credit now. %d < %d\n",
  678. pEndpoint->Id,
  679. pEndpoint->TxCredits,
  680. creditsRequired));
  681. #endif
  682. if (do_pm_get)
  683. hif_pm_runtime_put(target->hif_dev);
  684. break;
  685. }
  686. pEndpoint->TxCredits -= creditsRequired;
  687. INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
  688. creditsRequired);
  689. /* check if we need credits back from the target */
  690. if (pEndpoint->TxCredits <=
  691. pEndpoint->TxCreditsPerMaxMsg) {
  692. /* tell the target we need credits ASAP! */
  693. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  694. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  695. LOCK_HTC_CREDIT(target);
  696. htc_credit_record(HTC_REQUEST_CREDIT,
  697. pEndpoint->TxCredits,
  698. HTC_PACKET_QUEUE_DEPTH
  699. (tx_queue));
  700. UNLOCK_HTC_CREDIT(target);
  701. }
  702. INC_HTC_EP_STAT(pEndpoint,
  703. TxCreditLowIndications, 1);
  704. #if DEBUG_CREDIT
  705. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  706. (" EP%d Needs Credits\n",
  707. pEndpoint->Id));
  708. #endif
  709. }
  710. }
  711. /* now we can fully dequeue */
  712. pPacket = htc_packet_dequeue(tx_queue);
  713. if (pPacket) {
  714. /* save the number of credits this packet consumed */
  715. pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
  716. /* save send flags */
  717. pPacket->PktInfo.AsTx.SendFlags = sendFlags;
  718. /* queue this packet into the caller's queue */
  719. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  720. }
  721. }
  722. if (!HTC_QUEUE_EMPTY(&pm_queue))
  723. queue_htc_pm_packets(pEndpoint, &pm_queue);
  724. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  725. ("-get_htc_send_packets_credit_based\n"));
  726. }
  727. void get_htc_send_packets(HTC_TARGET *target,
  728. HTC_ENDPOINT *pEndpoint,
  729. HTC_PACKET_QUEUE *pQueue, int Resources)
  730. {
  731. HTC_PACKET *pPacket;
  732. HTC_PACKET_QUEUE *tx_queue;
  733. HTC_PACKET_QUEUE pm_queue;
  734. bool do_pm_get;
  735. /****** NOTE : the TX lock is held when this function is called *****************/
  736. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  737. ("+get_htc_send_packets %d resources\n", Resources));
  738. INIT_HTC_PACKET_QUEUE(&pm_queue);
  739. extract_htc_pm_packets(pEndpoint, &pm_queue);
  740. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  741. tx_queue = &pEndpoint->TxQueue;
  742. do_pm_get = true;
  743. } else {
  744. tx_queue = &pm_queue;
  745. }
  746. /* loop until we can grab as many packets out of the queue as we can */
  747. while (Resources > 0) {
  748. int num_frags;
  749. if (do_pm_get && hif_pm_runtime_get(target->hif_dev)) {
  750. /* bus suspended, runtime resume issued */
  751. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  752. break;
  753. }
  754. pPacket = htc_packet_dequeue(tx_queue);
  755. if (pPacket == NULL) {
  756. if (do_pm_get)
  757. hif_pm_runtime_put(target->hif_dev);
  758. break;
  759. }
  760. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  761. (" Got packet:%p , New Queue Depth: %d\n",
  762. pPacket,
  763. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  764. /* For non-credit path the sequence number is already embedded
  765. * in the constructed HTC header
  766. */
  767. #if 0
  768. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  769. pEndpoint->SeqNo++;
  770. #endif
  771. pPacket->PktInfo.AsTx.SendFlags = 0;
  772. pPacket->PktInfo.AsTx.CreditsUsed = 0;
  773. /* queue this packet into the caller's queue */
  774. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  775. /*
  776. * FIX THIS:
  777. * For now, avoid calling cdf_nbuf_get_num_frags before calling
  778. * cdf_nbuf_map, because the MacOS version of cdf_nbuf_t doesn't
  779. * support cdf_nbuf_get_num_frags until after cdf_nbuf_map has
  780. * been done.
  781. * Assume that the non-data netbufs, i.e. the WMI message netbufs,
  782. * consist of a single fragment.
  783. */
  784. num_frags =
  785. (pPacket->PktInfo.AsTx.
  786. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1
  787. /* WMI messages are in a single-fragment network buffer */ :
  788. cdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
  789. (pPacket));
  790. Resources -= num_frags;
  791. }
  792. if (!HTC_QUEUE_EMPTY(&pm_queue))
  793. queue_htc_pm_packets(pEndpoint, &pm_queue);
  794. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets\n"));
  795. }
  796. static HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
  797. HTC_ENDPOINT *pEndpoint,
  798. HTC_PACKET_QUEUE *pCallersSendQueue)
  799. {
  800. HTC_PACKET_QUEUE sendQueue; /* temp queue to hold packets at various stages */
  801. HTC_PACKET *pPacket;
  802. int tx_resources;
  803. int overflow;
  804. HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
  805. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%p Depth:%d)\n",
  806. pCallersSendQueue,
  807. (pCallersSendQueue ==
  808. NULL) ? 0 :
  809. HTC_PACKET_QUEUE_DEPTH
  810. (pCallersSendQueue)));
  811. /* init the local send queue */
  812. INIT_HTC_PACKET_QUEUE(&sendQueue);
  813. do {
  814. if (NULL == pCallersSendQueue) {
  815. /* caller didn't provide a queue, just wants us to check queues and send */
  816. break;
  817. }
  818. if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
  819. /* empty queue */
  820. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  821. HTC_PKT_Q_EMPTY);
  822. result = HTC_SEND_QUEUE_DROP;
  823. break;
  824. }
  825. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
  826. pEndpoint->MaxTxQueueDepth) {
  827. /* we've already overflowed */
  828. overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  829. } else {
  830. /* figure out how much we will overflow by */
  831. overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  832. overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  833. /* figure out how much we will overflow the TX queue by */
  834. overflow -= pEndpoint->MaxTxQueueDepth;
  835. }
  836. /* if overflow is negative or zero, we are okay */
  837. if (overflow > 0) {
  838. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  839. (" Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d \n",
  840. pEndpoint->Id, overflow,
  841. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  842. TxQueue),
  843. pEndpoint->MaxTxQueueDepth));
  844. }
  845. if ((overflow <= 0)
  846. || (pEndpoint->EpCallBacks.EpSendFull == NULL)) {
  847. /* all packets will fit or caller did not provide send full indication handler
  848. * -- just move all of them to the local sendQueue object */
  849. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
  850. pCallersSendQueue);
  851. } else {
  852. int i;
  853. int goodPkts =
  854. HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
  855. overflow;
  856. A_ASSERT(goodPkts >= 0);
  857. /* we have overflowed, and a callback is provided */
  858. /* dequeue all non-overflow packets into the sendqueue */
  859. for (i = 0; i < goodPkts; i++) {
  860. /* pop off caller's queue */
  861. pPacket = htc_packet_dequeue(pCallersSendQueue);
  862. A_ASSERT(pPacket != NULL);
  863. /* insert into local queue */
  864. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  865. }
  866. /* the caller's queue has all the packets that won't fit */
  867. /* walk through the caller's queue and indicate each one to the send full handler */
  868. ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
  869. QueueHead, pPacket,
  870. HTC_PACKET, ListLink) {
  871. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  872. (" Indicating overflowed TX packet: %p \n",
  873. pPacket));
  874. /*
  875. * Remove headroom reserved for HTC_FRAME_HDR before giving
  876. * the packet back to the user via the EpSendFull callback.
  877. */
  878. restore_tx_packet(target, pPacket);
  879. if (pEndpoint->EpCallBacks.
  880. EpSendFull(pEndpoint->EpCallBacks.pContext,
  881. pPacket) == HTC_SEND_FULL_DROP) {
  882. /* callback wants the packet dropped */
  883. INC_HTC_EP_STAT(pEndpoint, TxDropped,
  884. 1);
  885. /* leave this one in the caller's queue for cleanup */
  886. } else {
  887. /* callback wants to keep this packet, remove from caller's queue */
  888. HTC_PACKET_REMOVE(pCallersSendQueue,
  889. pPacket);
  890. /* put it in the send queue */
  891. /* add HTC_FRAME_HDR space reservation again */
  892. cdf_nbuf_push_head
  893. (GET_HTC_PACKET_NET_BUF_CONTEXT
  894. (pPacket), sizeof(HTC_FRAME_HDR));
  895. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  896. }
  897. }
  898. ITERATE_END;
  899. if (HTC_QUEUE_EMPTY(&sendQueue)) {
  900. /* no packets made it in, caller will cleanup */
  901. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  902. HTC_SEND_Q_EMPTY);
  903. result = HTC_SEND_QUEUE_DROP;
  904. break;
  905. }
  906. }
  907. } while (false);
  908. if (result != HTC_SEND_QUEUE_OK) {
  909. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: \n"));
  910. return result;
  911. }
  912. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  913. tx_resources =
  914. hif_get_free_queue_number(target->hif_dev,
  915. pEndpoint->UL_PipeID);
  916. } else {
  917. tx_resources = 0;
  918. }
  919. LOCK_HTC_TX(target);
  920. if (!HTC_QUEUE_EMPTY(&sendQueue)) {
  921. /* transfer packets to tail */
  922. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
  923. &sendQueue);
  924. A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
  925. INIT_HTC_PACKET_QUEUE(&sendQueue);
  926. }
  927. /* increment tx processing count on entry */
  928. qdf_atomic_inc(&pEndpoint->TxProcessCount);
  929. if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  930. /* another thread or task is draining the TX queues on this endpoint
  931. * that thread will reset the tx processing count when the queue is drained */
  932. qdf_atomic_dec(&pEndpoint->TxProcessCount);
  933. UNLOCK_HTC_TX(target);
  934. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy) \n"));
  935. return HTC_SEND_QUEUE_OK;
  936. }
  937. /***** beyond this point only 1 thread may enter ******/
  938. /* now drain the endpoint TX queue for transmission as long as we have enough
  939. * transmit resources */
  940. while (true) {
  941. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0) {
  942. break;
  943. }
  944. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  945. #if DEBUG_CREDIT
  946. int cred = pEndpoint->TxCredits;
  947. #endif
  948. /* credit based mechanism provides flow control based on target transmit resource availability, we
  949. * assume that the HIF layer will always have bus resources greater than target transmit resources */
  950. get_htc_send_packets_credit_based(target, pEndpoint,
  951. &sendQueue);
  952. #if DEBUG_CREDIT
  953. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  954. if (cred - pEndpoint->TxCredits > 0) {
  955. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  956. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  957. pEndpoint->Id, cred,
  958. cred -
  959. pEndpoint->TxCredits,
  960. pEndpoint->TxCredits));
  961. }
  962. }
  963. #endif
  964. } else {
  965. /* get all the packets for this endpoint that we can for this pass */
  966. get_htc_send_packets(target, pEndpoint, &sendQueue,
  967. tx_resources);
  968. }
  969. if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
  970. /* didn't get any packets due to a lack of resources or TX queue was drained */
  971. break;
  972. }
  973. UNLOCK_HTC_TX(target);
  974. /* send what we can */
  975. result = htc_issue_packets(target, pEndpoint, &sendQueue);
  976. if (result) {
  977. int i;
  978. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  979. ("htc_issue_packets, failed status:%d put it back to head of callersSendQueue",
  980. result));
  981. for (i = HTC_PACKET_QUEUE_DEPTH(&sendQueue); i > 0; i--)
  982. hif_pm_runtime_put(target->hif_dev);
  983. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  984. &sendQueue);
  985. LOCK_HTC_TX(target);
  986. break;
  987. }
  988. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  989. tx_resources =
  990. hif_get_free_queue_number(target->hif_dev,
  991. pEndpoint->UL_PipeID);
  992. }
  993. LOCK_HTC_TX(target);
  994. }
  995. UNLOCK_HTC_TX(target);
  996. /* done with this endpoint, we can clear the count */
  997. qdf_atomic_init(&pEndpoint->TxProcessCount);
  998. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: \n"));
  999. return HTC_SEND_QUEUE_OK;
  1000. }
  1001. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1002. static A_UINT16 htc_send_pkts_sched_check(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID id)
  1003. {
  1004. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1005. HTC_ENDPOINT *pEndpoint;
  1006. HTC_ENDPOINT_ID eid;
  1007. HTC_PACKET_QUEUE *pTxQueue;
  1008. A_UINT16 resources;
  1009. A_UINT16 acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
  1010. if (id < ENDPOINT_2 || id > ENDPOINT_5) {
  1011. return 1;
  1012. }
  1013. for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
  1014. pEndpoint = &target->endpoint[eid];
  1015. pTxQueue = &pEndpoint->TxQueue;
  1016. if (HTC_QUEUE_EMPTY(pTxQueue)) {
  1017. acQueueStatus[eid - 2] = 1;
  1018. }
  1019. }
  1020. switch (id) {
  1021. case ENDPOINT_2: /* BE */
  1022. return acQueueStatus[0] && acQueueStatus[2]
  1023. && acQueueStatus[3];
  1024. case ENDPOINT_3: /* BK */
  1025. return acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
  1026. && acQueueStatus[3];
  1027. case ENDPOINT_4: /* VI */
  1028. return acQueueStatus[2] && acQueueStatus[3];
  1029. case ENDPOINT_5: /* VO */
  1030. return acQueueStatus[3];
  1031. default:
  1032. return 0;
  1033. }
  1034. }
  1035. static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
  1036. HTC_PACKET_QUEUE *pPktQueue,
  1037. HTC_ENDPOINT_ID eid)
  1038. {
  1039. HTC_ENDPOINT *pEndpoint;
  1040. HTC_PACKET_QUEUE *pTxQueue;
  1041. HTC_PACKET *pPacket;
  1042. int goodPkts;
  1043. pEndpoint = &target->endpoint[eid];
  1044. pTxQueue = &pEndpoint->TxQueue;
  1045. LOCK_HTC_TX(target);
  1046. goodPkts =
  1047. pEndpoint->MaxTxQueueDepth -
  1048. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  1049. if (goodPkts > 0) {
  1050. while (!HTC_QUEUE_EMPTY(pPktQueue)) {
  1051. pPacket = htc_packet_dequeue(pPktQueue);
  1052. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  1053. goodPkts--;
  1054. if (goodPkts <= 0) {
  1055. break;
  1056. }
  1057. }
  1058. }
  1059. if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
  1060. ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
  1061. HTC_PACKET, ListLink) {
  1062. if (pEndpoint->EpCallBacks.
  1063. EpSendFull(pEndpoint->EpCallBacks.pContext,
  1064. pPacket) == HTC_SEND_FULL_DROP) {
  1065. INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
  1066. } else {
  1067. HTC_PACKET_REMOVE(pPktQueue, pPacket);
  1068. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  1069. }
  1070. }
  1071. ITERATE_END;
  1072. }
  1073. UNLOCK_HTC_TX(target);
  1074. return A_OK;
  1075. }
  1076. #endif
  1077. A_STATUS htc_send_pkts_multiple(HTC_HANDLE HTCHandle, HTC_PACKET_QUEUE *pPktQueue)
  1078. {
  1079. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1080. HTC_ENDPOINT *pEndpoint;
  1081. HTC_PACKET *pPacket;
  1082. cdf_nbuf_t netbuf;
  1083. HTC_FRAME_HDR *pHtcHdr;
  1084. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1085. ("+htc_send_pkts_multiple: Queue: %p, Pkts %d \n",
  1086. pPktQueue, HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
  1087. /* get packet at head to figure out which endpoint these packets will go into */
  1088. pPacket = htc_get_pkt_at_head(pPktQueue);
  1089. if (NULL == pPacket) {
  1090. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
  1091. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_send_pkts_multiple \n"));
  1092. return A_EINVAL;
  1093. }
  1094. AR_DEBUG_ASSERT(pPacket->Endpoint < ENDPOINT_MAX);
  1095. pEndpoint = &target->endpoint[pPacket->Endpoint];
  1096. if (!pEndpoint->service_id) {
  1097. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
  1098. __func__));
  1099. return A_EINVAL;
  1100. }
  1101. #ifdef HTC_EP_STAT_PROFILING
  1102. LOCK_HTC_TX(target);
  1103. INC_HTC_EP_STAT(pEndpoint, TxPosted, HTC_PACKET_QUEUE_DEPTH(pPktQueue));
  1104. UNLOCK_HTC_TX(target);
  1105. #endif
  1106. /* provide room in each packet's netbuf for the HTC frame header */
  1107. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue, pPacket) {
  1108. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1109. AR_DEBUG_ASSERT(netbuf);
  1110. cdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
  1111. /* setup HTC frame header */
  1112. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  1113. AR_DEBUG_ASSERT(pHtcHdr);
  1114. HTC_WRITE32(pHtcHdr,
  1115. SM(pPacket->ActualLength,
  1116. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->Endpoint,
  1117. HTC_FRAME_HDR_ENDPOINTID));
  1118. LOCK_HTC_TX(target);
  1119. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1120. pEndpoint->SeqNo++;
  1121. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1122. SM(pPacket->PktInfo.AsTx.SeqNo,
  1123. HTC_FRAME_HDR_CONTROLBYTES1));
  1124. UNLOCK_HTC_TX(target);
  1125. /*
  1126. * Now that the HTC frame header has been added, the netbuf can be
  1127. * mapped. This only applies to non-data frames, since data frames
  1128. * were already mapped as they entered into the driver.
  1129. */
  1130. cdf_nbuf_map(target->osdev,
  1131. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  1132. QDF_DMA_TO_DEVICE);
  1133. pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  1134. }
  1135. HTC_PACKET_QUEUE_ITERATE_END;
  1136. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1137. if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id)) {
  1138. htc_send_pkts_sched_queue(HTCHandle, pPktQueue, pEndpoint->Id);
  1139. } else {
  1140. htc_try_send(target, pEndpoint, pPktQueue);
  1141. }
  1142. #else
  1143. htc_try_send(target, pEndpoint, pPktQueue);
  1144. #endif
  1145. /* do completion on any packets that couldn't get in */
  1146. if (!HTC_QUEUE_EMPTY(pPktQueue)) {
  1147. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue, pPacket) {
  1148. /* remove the headroom reserved for HTC_FRAME_HDR */
  1149. restore_tx_packet(target, pPacket);
  1150. if (HTC_STOPPING(target)) {
  1151. pPacket->Status = A_ECANCELED;
  1152. } else {
  1153. pPacket->Status = A_NO_RESOURCE;
  1154. }
  1155. }
  1156. HTC_PACKET_QUEUE_ITERATE_END;
  1157. do_send_completion(pEndpoint, pPktQueue);
  1158. }
  1159. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_send_pkts_multiple \n"));
  1160. return A_OK;
  1161. }
  1162. /* HTC API - htc_send_pkt */
  1163. A_STATUS htc_send_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket)
  1164. {
  1165. HTC_PACKET_QUEUE queue;
  1166. if (HTCHandle == NULL || pPacket == NULL) {
  1167. return A_ERROR;
  1168. }
  1169. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1170. ("+-htc_send_pkt: Enter endPointId: %d, buffer: %p, length: %d \n",
  1171. pPacket->Endpoint, pPacket->pBuffer,
  1172. pPacket->ActualLength));
  1173. INIT_HTC_PACKET_QUEUE_AND_ADD(&queue, pPacket);
  1174. return htc_send_pkts_multiple(HTCHandle, &queue);
  1175. }
  1176. #ifdef ATH_11AC_TXCOMPACT
  1177. A_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, cdf_nbuf_t netbuf, int Epid,
  1178. int ActualLength)
  1179. {
  1180. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1181. HTC_ENDPOINT *pEndpoint;
  1182. HTC_FRAME_HDR *pHtcHdr;
  1183. A_STATUS status = A_OK;
  1184. int tx_resources;
  1185. uint32_t data_attr = 0;
  1186. pEndpoint = &target->endpoint[Epid];
  1187. tx_resources =
  1188. hif_get_free_queue_number(target->hif_dev, pEndpoint->UL_PipeID);
  1189. if (tx_resources < HTC_DATA_RESOURCE_THRS) {
  1190. if (pEndpoint->ul_is_polled) {
  1191. hif_send_complete_check(pEndpoint->target->hif_dev,
  1192. pEndpoint->UL_PipeID, 1);
  1193. tx_resources =
  1194. hif_get_free_queue_number(target->hif_dev,
  1195. pEndpoint->UL_PipeID);
  1196. }
  1197. if (tx_resources < HTC_DATA_MINDESC_PERPACKET) {
  1198. return A_ERROR;
  1199. }
  1200. }
  1201. if (hif_pm_runtime_get(target->hif_dev))
  1202. return A_ERROR;
  1203. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  1204. AR_DEBUG_ASSERT(pHtcHdr);
  1205. data_attr = cdf_nbuf_data_attr_get(netbuf);
  1206. HTC_WRITE32(pHtcHdr, SM(ActualLength, HTC_FRAME_HDR_PAYLOADLEN) |
  1207. SM(Epid, HTC_FRAME_HDR_ENDPOINTID));
  1208. /*
  1209. * If the HIF pipe for the data endpoint is polled rather than
  1210. * interrupt-driven, this is a good point to check whether any
  1211. * data previously sent through the HIF pipe have finished being
  1212. * sent.
  1213. * Since this may result in callbacks to htc_tx_completion_handler,
  1214. * which can take the HTC tx lock, make the hif_send_complete_check
  1215. * call before acquiring the HTC tx lock.
  1216. * Call hif_send_complete_check directly, rather than calling
  1217. * htc_send_complete_check, and call the PollTimerStart separately
  1218. * after calling hif_send_head, so the timer will be started to
  1219. * check for completion of the new outstanding download (in the
  1220. * unexpected event that other polling calls don't catch it).
  1221. */
  1222. LOCK_HTC_TX(target);
  1223. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1224. SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
  1225. pEndpoint->SeqNo++;
  1226. NBUF_UPDATE_TX_PKT_COUNT(netbuf, NBUF_TX_PKT_HTC);
  1227. DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1228. (uint8_t *)(cdf_nbuf_data(netbuf)),
  1229. sizeof(cdf_nbuf_data(netbuf))));
  1230. status = hif_send_head(target->hif_dev,
  1231. pEndpoint->UL_PipeID,
  1232. pEndpoint->Id, ActualLength, netbuf, data_attr);
  1233. UNLOCK_HTC_TX(target);
  1234. return status;
  1235. }
  1236. #else /*ATH_11AC_TXCOMPACT */
  1237. A_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
  1238. A_UINT8 more_data)
  1239. {
  1240. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1241. HTC_ENDPOINT *pEndpoint;
  1242. HTC_FRAME_HDR *pHtcHdr;
  1243. HTC_PACKET_QUEUE sendQueue;
  1244. cdf_nbuf_t netbuf = NULL;
  1245. int tx_resources;
  1246. A_STATUS status = A_OK;
  1247. uint32_t data_attr = 0;
  1248. if (pPacket) {
  1249. AR_DEBUG_ASSERT(pPacket->Endpoint < ENDPOINT_MAX);
  1250. pEndpoint = &target->endpoint[pPacket->Endpoint];
  1251. /* add HTC_FRAME_HDR in the initial fragment */
  1252. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1253. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  1254. AR_DEBUG_ASSERT(pHtcHdr);
  1255. HTC_WRITE32(pHtcHdr,
  1256. SM(pPacket->ActualLength,
  1257. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->PktInfo.
  1258. AsTx.SendFlags,
  1259. HTC_FRAME_HDR_FLAGS)
  1260. | SM(pPacket->Endpoint, HTC_FRAME_HDR_ENDPOINTID));
  1261. /*
  1262. * If the HIF pipe for the data endpoint is polled rather than
  1263. * interrupt-driven, this is a good point to check whether any
  1264. * data previously sent through the HIF pipe have finished being
  1265. * sent.
  1266. * Since this may result in callbacks to htc_tx_completion_handler,
  1267. * which can take the HTC tx lock, make the hif_send_complete_check
  1268. * call before acquiring the HTC tx lock.
  1269. * Call hif_send_complete_check directly, rather than calling
  1270. * htc_send_complete_check, and call the PollTimerStart separately
  1271. * after calling hif_send_head, so the timer will be started to
  1272. * check for completion of the new outstanding download (in the
  1273. * unexpected event that other polling calls don't catch it).
  1274. */
  1275. if (pEndpoint->ul_is_polled) {
  1276. htc_send_complete_poll_timer_stop(pEndpoint);
  1277. hif_send_complete_check(pEndpoint->target->hif_dev,
  1278. pEndpoint->UL_PipeID, 0);
  1279. }
  1280. LOCK_HTC_TX(target);
  1281. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1282. pEndpoint->SeqNo++;
  1283. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1284. SM(pPacket->PktInfo.AsTx.SeqNo,
  1285. HTC_FRAME_HDR_CONTROLBYTES1));
  1286. /* append new packet to pEndpoint->TxQueue */
  1287. HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
  1288. #ifdef ENABLE_BUNDLE_TX
  1289. if (HTC_ENABLE_BUNDLE(target) && (more_data)) {
  1290. UNLOCK_HTC_TX(target);
  1291. return A_OK;
  1292. }
  1293. #endif
  1294. } else {
  1295. LOCK_HTC_TX(target);
  1296. pEndpoint = &target->endpoint[1];
  1297. }
  1298. /* increment tx processing count on entry */
  1299. qdf_atomic_inc(&pEndpoint->TxProcessCount);
  1300. if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  1301. /*
  1302. * Another thread or task is draining the TX queues on this endpoint.
  1303. * That thread will reset the tx processing count when the queue is
  1304. * drained.
  1305. */
  1306. qdf_atomic_dec(&pEndpoint->TxProcessCount);
  1307. UNLOCK_HTC_TX(target);
  1308. return A_OK;
  1309. }
  1310. /***** beyond this point only 1 thread may enter ******/
  1311. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1312. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1313. #if DEBUG_CREDIT
  1314. int cred = pEndpoint->TxCredits;
  1315. #endif
  1316. get_htc_send_packets_credit_based(target, pEndpoint, &sendQueue);
  1317. #if DEBUG_CREDIT
  1318. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1319. if (cred - pEndpoint->TxCredits > 0) {
  1320. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1321. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  1322. pEndpoint->Id, cred,
  1323. cred - pEndpoint->TxCredits,
  1324. pEndpoint->TxCredits));
  1325. }
  1326. }
  1327. #endif
  1328. UNLOCK_HTC_TX(target);
  1329. }
  1330. #ifdef ENABLE_BUNDLE_TX
  1331. else if (HTC_ENABLE_BUNDLE(target)) {
  1332. /* Dequeue max packets from endpoint tx queue */
  1333. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1334. HTC_MAX_TX_BUNDLE_SEND_LIMIT);
  1335. UNLOCK_HTC_TX(target);
  1336. }
  1337. #endif
  1338. else {
  1339. /*
  1340. * Now drain the endpoint TX queue for transmission as long as we have
  1341. * enough transmit resources
  1342. */
  1343. tx_resources =
  1344. hif_get_free_queue_number(target->hif_dev,
  1345. pEndpoint->UL_PipeID);
  1346. get_htc_send_packets(target, pEndpoint, &sendQueue, tx_resources);
  1347. UNLOCK_HTC_TX(target);
  1348. }
  1349. NBUF_UPDATE_TX_PKT_COUNT(netbuf, NBUF_TX_PKT_HTC);
  1350. DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1351. (uint8_t *)(cdf_nbuf_data(netbuf)),
  1352. sizeof(cdf_nbuf_data(netbuf))));
  1353. /* send what we can */
  1354. while (true) {
  1355. #if defined(HIF_USB) || defined(HIF_SDIO)
  1356. #ifdef ENABLE_BUNDLE_TX
  1357. if (HTC_ENABLE_BUNDLE(target) &&
  1358. HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
  1359. HTC_MIN_MSG_PER_BUNDLE) {
  1360. htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
  1361. }
  1362. #endif
  1363. #endif
  1364. pPacket = htc_packet_dequeue(&sendQueue);
  1365. if (pPacket == NULL) {
  1366. break;
  1367. }
  1368. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1369. LOCK_HTC_TX(target);
  1370. /* store in look up queue to match completions */
  1371. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  1372. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  1373. pEndpoint->ul_outstanding_cnt++;
  1374. UNLOCK_HTC_TX(target);
  1375. status = hif_send_head(target->hif_dev,
  1376. pEndpoint->UL_PipeID,
  1377. pEndpoint->Id,
  1378. HTC_HDR_LENGTH + pPacket->ActualLength,
  1379. netbuf, data_attr);
  1380. #if DEBUG_BUNDLE
  1381. qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.\n",
  1382. pEndpoint->Id,
  1383. pEndpoint->TxCreditSize,
  1384. HTC_HDR_LENGTH + pPacket->ActualLength);
  1385. #endif
  1386. if (qdf_unlikely(A_FAILED(status))) {
  1387. LOCK_HTC_TX(target);
  1388. pEndpoint->ul_outstanding_cnt--;
  1389. /* remove this packet from the tx completion queue */
  1390. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  1391. /*
  1392. * Don't bother reclaiming credits - HTC flow control
  1393. * is not applicable to tx data.
  1394. * In LL systems, there is no download flow control,
  1395. * since there's virtually no download delay.
  1396. * In HL systems, the txrx SW explicitly performs the
  1397. * tx flow control.
  1398. */
  1399. /* pEndpoint->TxCredits += pPacket->PktInfo.AsTx.CreditsUsed; */
  1400. /* put this frame back at the front of the sendQueue */
  1401. HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
  1402. /* put the sendQueue back at the front of pEndpoint->TxQueue */
  1403. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1404. &sendQueue);
  1405. UNLOCK_HTC_TX(target);
  1406. break; /* still need to reset TxProcessCount */
  1407. }
  1408. }
  1409. /* done with this endpoint, we can clear the count */
  1410. qdf_atomic_init(&pEndpoint->TxProcessCount);
  1411. if (pEndpoint->ul_is_polled) {
  1412. /*
  1413. * Start a cleanup timer to poll for download completion.
  1414. * The download completion should be noticed promptly from
  1415. * other polling calls, but the timer provides a safety net
  1416. * in case other polling calls don't occur as expected.
  1417. */
  1418. htc_send_complete_poll_timer_start(pEndpoint);
  1419. }
  1420. return status;
  1421. }
  1422. #endif /*ATH_11AC_TXCOMPACT */
  1423. /*
  1424. * In the adapted HIF layer, cdf_nbuf_t are passed between HIF and HTC, since upper layers expects
  1425. * HTC_PACKET containers we use the completed netbuf and lookup its corresponding HTC packet buffer
  1426. * from a lookup list.
  1427. * This is extra overhead that can be fixed by re-aligning HIF interfaces with HTC.
  1428. *
  1429. */
  1430. static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
  1431. HTC_ENDPOINT *pEndpoint,
  1432. cdf_nbuf_t netbuf)
  1433. {
  1434. HTC_PACKET *pPacket = NULL;
  1435. HTC_PACKET *pFoundPacket = NULL;
  1436. HTC_PACKET_QUEUE lookupQueue;
  1437. INIT_HTC_PACKET_QUEUE(&lookupQueue);
  1438. LOCK_HTC_TX(target);
  1439. /* mark that HIF has indicated the send complete for another packet */
  1440. pEndpoint->ul_outstanding_cnt--;
  1441. /* Dequeue first packet directly because of in-order completion */
  1442. pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
  1443. if (qdf_unlikely(!pPacket)) {
  1444. UNLOCK_HTC_TX(target);
  1445. return NULL;
  1446. }
  1447. if (netbuf == (cdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  1448. UNLOCK_HTC_TX(target);
  1449. return pPacket;
  1450. } else {
  1451. HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
  1452. }
  1453. /*
  1454. * Move TX lookup queue to temp queue because most of packets that are not index 0
  1455. * are not top 10 packets.
  1456. */
  1457. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
  1458. &pEndpoint->TxLookupQueue);
  1459. UNLOCK_HTC_TX(target);
  1460. ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
  1461. HTC_PACKET, ListLink) {
  1462. if (NULL == pPacket) {
  1463. pFoundPacket = pPacket;
  1464. break;
  1465. }
  1466. /* check for removal */
  1467. if (netbuf ==
  1468. (cdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  1469. /* found it */
  1470. HTC_PACKET_REMOVE(&lookupQueue, pPacket);
  1471. pFoundPacket = pPacket;
  1472. break;
  1473. }
  1474. }
  1475. ITERATE_END;
  1476. LOCK_HTC_TX(target);
  1477. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
  1478. &lookupQueue);
  1479. UNLOCK_HTC_TX(target);
  1480. return pFoundPacket;
  1481. }
  1482. QDF_STATUS htc_tx_completion_handler(void *Context,
  1483. cdf_nbuf_t netbuf, unsigned int EpID,
  1484. uint32_t toeplitz_hash_result)
  1485. {
  1486. HTC_TARGET *target = (HTC_TARGET *) Context;
  1487. HTC_ENDPOINT *pEndpoint;
  1488. HTC_PACKET *pPacket;
  1489. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1490. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5, ENDPOINT_4,
  1491. ENDPOINT_2, ENDPOINT_3 };
  1492. int epidIdx;
  1493. A_UINT16 resourcesThresh[DATA_EP_SIZE]; /* urb resources */
  1494. A_UINT16 resources;
  1495. A_UINT16 resourcesMax;
  1496. #endif
  1497. pEndpoint = &target->endpoint[EpID];
  1498. target->TX_comp_cnt++;
  1499. do {
  1500. pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
  1501. if (NULL == pPacket) {
  1502. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1503. ("HTC TX lookup failed!\n"));
  1504. /* may have already been flushed and freed */
  1505. netbuf = NULL;
  1506. break;
  1507. }
  1508. if (pPacket->PktInfo.AsTx.Tag != HTC_TX_PACKET_TAG_AUTO_PM)
  1509. hif_pm_runtime_put(target->hif_dev);
  1510. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
  1511. HTC_PACKET *pPacketTemp;
  1512. HTC_PACKET_QUEUE *pQueueSave =
  1513. (HTC_PACKET_QUEUE *) pPacket->pContext;
  1514. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
  1515. pPacketTemp) {
  1516. pPacket->Status = A_OK;
  1517. send_packet_completion(target, pPacketTemp);
  1518. }
  1519. HTC_PACKET_QUEUE_ITERATE_END;
  1520. free_htc_bundle_packet(target, pPacket);
  1521. return QDF_STATUS_SUCCESS;
  1522. }
  1523. /* will be giving this buffer back to upper layers */
  1524. netbuf = NULL;
  1525. pPacket->Status = QDF_STATUS_SUCCESS;
  1526. send_packet_completion(target, pPacket);
  1527. } while (false);
  1528. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1529. /* note: when using TX credit flow, the re-checking of queues happens
  1530. * when credits flow back from the target.
  1531. * in the non-TX credit case, we recheck after the packet completes */
  1532. htc_try_send(target, pEndpoint, NULL);
  1533. }
  1534. return QDF_STATUS_SUCCESS;
  1535. }
  1536. /* callback when TX resources become available */
  1537. void htc_tx_resource_avail_handler(void *context, A_UINT8 pipeID)
  1538. {
  1539. int i;
  1540. HTC_TARGET *target = (HTC_TARGET *) context;
  1541. HTC_ENDPOINT *pEndpoint = NULL;
  1542. for (i = 0; i < ENDPOINT_MAX; i++) {
  1543. pEndpoint = &target->endpoint[i];
  1544. if (pEndpoint->service_id != 0) {
  1545. if (pEndpoint->UL_PipeID == pipeID) {
  1546. break;
  1547. }
  1548. }
  1549. }
  1550. if (i >= ENDPOINT_MAX) {
  1551. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1552. ("Invalid pipe indicated for TX resource avail : %d!\n",
  1553. pipeID));
  1554. return;
  1555. }
  1556. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1557. ("HIF indicated more resources for pipe:%d \n",
  1558. pipeID));
  1559. htc_try_send(target, pEndpoint, NULL);
  1560. }
  1561. /**
  1562. * htc_kick_queues(): resumes tx transactions of suspended endpoints
  1563. * @context: pointer to the htc target context
  1564. *
  1565. * Iterates throught the enpoints and provides a context to empty queues
  1566. * int the hif layer when they are stalled due to runtime suspend.
  1567. *
  1568. * Return: none
  1569. */
  1570. void htc_kick_queues(void *context)
  1571. {
  1572. int i;
  1573. HTC_TARGET *target = (HTC_TARGET *)context;
  1574. HTC_ENDPOINT *endpoint = NULL;
  1575. for (i = 0; i < ENDPOINT_MAX; i++) {
  1576. endpoint = &target->endpoint[i];
  1577. if (endpoint->service_id == 0)
  1578. continue;
  1579. if (endpoint->EpCallBacks.ep_resume_tx_queue)
  1580. endpoint->EpCallBacks.ep_resume_tx_queue(
  1581. endpoint->EpCallBacks.pContext);
  1582. htc_try_send(target, endpoint, NULL);
  1583. }
  1584. }
  1585. /* flush endpoint TX queue */
  1586. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  1587. HTC_TX_TAG Tag)
  1588. {
  1589. HTC_PACKET *pPacket;
  1590. LOCK_HTC_TX(target);
  1591. while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1592. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  1593. if (pPacket) {
  1594. /* let the sender know the packet was not delivered */
  1595. pPacket->Status = A_ECANCELED;
  1596. send_packet_completion(target, pPacket);
  1597. }
  1598. }
  1599. UNLOCK_HTC_TX(target);
  1600. }
  1601. /* HTC API to flush an endpoint's TX queue*/
  1602. void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
  1603. HTC_TX_TAG Tag)
  1604. {
  1605. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1606. HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
  1607. if (pEndpoint->service_id == 0) {
  1608. AR_DEBUG_ASSERT(false);
  1609. /* not in use.. */
  1610. return;
  1611. }
  1612. htc_flush_endpoint_tx(target, pEndpoint, Tag);
  1613. }
  1614. /* HTC API to indicate activity to the credit distribution function */
  1615. void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
  1616. HTC_ENDPOINT_ID Endpoint, A_BOOL Active)
  1617. {
  1618. /* TODO */
  1619. }
  1620. A_BOOL htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
  1621. {
  1622. return true;
  1623. }
  1624. /* process credit reports and call distribution function */
  1625. void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
  1626. int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
  1627. {
  1628. int i;
  1629. HTC_ENDPOINT *pEndpoint;
  1630. int totalCredits = 0;
  1631. A_UINT8 rpt_credits, rpt_ep_id;
  1632. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1633. ("+htc_process_credit_rpt, Credit Report Entries:%d \n",
  1634. NumEntries));
  1635. /* lock out TX while we update credits */
  1636. LOCK_HTC_TX(target);
  1637. for (i = 0; i < NumEntries; i++, pRpt++) {
  1638. rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
  1639. if (rpt_ep_id >= ENDPOINT_MAX) {
  1640. AR_DEBUG_ASSERT(false);
  1641. break;
  1642. }
  1643. rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
  1644. pEndpoint = &target->endpoint[rpt_ep_id];
  1645. #if DEBUG_CREDIT
  1646. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1647. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1648. (" <HTC> Increase EP%d %d + %d = %d credits\n",
  1649. rpt_ep_id, pEndpoint->TxCredits,
  1650. rpt_credits,
  1651. pEndpoint->TxCredits + rpt_credits));
  1652. }
  1653. #endif
  1654. #ifdef HTC_EP_STAT_PROFILING
  1655. INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
  1656. INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
  1657. if (FromEndpoint == rpt_ep_id) {
  1658. /* this credit report arrived on the same endpoint indicating it arrived in an RX
  1659. * packet */
  1660. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
  1661. rpt_credits);
  1662. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
  1663. } else if (FromEndpoint == ENDPOINT_0) {
  1664. /* this credit arrived on endpoint 0 as a NULL message */
  1665. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
  1666. rpt_credits);
  1667. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
  1668. } else {
  1669. /* arrived on another endpoint */
  1670. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
  1671. rpt_credits);
  1672. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
  1673. }
  1674. #endif
  1675. #if defined(HIF_USB)
  1676. if (pEndpoint->Id >= ENDPOINT_2 && pEndpoint->Id <= ENDPOINT_5) {
  1677. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5,
  1678. ENDPOINT_4, ENDPOINT_2, ENDPOINT_3 };
  1679. int epid_idx;
  1680. target->avail_tx_credits += rpt_credits;
  1681. for (epid_idx = 0; epid_idx < DATA_EP_SIZE; epid_idx++) {
  1682. pEndpoint = &target->endpoint[eid[epid_idx]];
  1683. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1684. break;
  1685. }
  1686. }
  1687. UNLOCK_HTC_TX(target);
  1688. htc_try_send(target, pEndpoint, NULL);
  1689. LOCK_HTC_TX(target);
  1690. } else {
  1691. pEndpoint->TxCredits += rpt_credits;
  1692. if (pEndpoint->TxCredits
  1693. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1694. UNLOCK_HTC_TX(target);
  1695. htc_try_send(target, pEndpoint, NULL);
  1696. LOCK_HTC_TX(target);
  1697. }
  1698. }
  1699. #else
  1700. pEndpoint->TxCredits += rpt_credits;
  1701. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  1702. LOCK_HTC_CREDIT(target);
  1703. htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
  1704. pEndpoint->TxCredits,
  1705. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  1706. TxQueue));
  1707. UNLOCK_HTC_CREDIT(target);
  1708. }
  1709. if (pEndpoint->TxCredits
  1710. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1711. UNLOCK_HTC_TX(target);
  1712. #ifdef ATH_11AC_TXCOMPACT
  1713. htc_try_send(target, pEndpoint, NULL);
  1714. #else
  1715. if (pEndpoint->service_id == HTT_DATA_MSG_SVC) {
  1716. htc_send_data_pkt(target, NULL, 0);
  1717. } else {
  1718. htc_try_send(target, pEndpoint, NULL);
  1719. }
  1720. #endif
  1721. LOCK_HTC_TX(target);
  1722. }
  1723. #endif
  1724. totalCredits += rpt_credits;
  1725. }
  1726. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1727. (" Report indicated %d credits to distribute \n",
  1728. totalCredits));
  1729. UNLOCK_HTC_TX(target);
  1730. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt \n"));
  1731. }
  1732. /* function to fetch stats from htc layer*/
  1733. struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
  1734. {
  1735. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1736. return &(target->htc_pkt_stats);
  1737. }