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 <qdf_nbuf.h> /* qdf_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. qdf_nbuf_t netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  120. qdf_nbuf_unmap(target->osdev, netbuf, QDF_DMA_TO_DEVICE);
  121. qdf_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. qdf_nbuf_t netbuf;
  181. LOCK_HTC_TX(target);
  182. if (NULL == target->pBundleFreeList) {
  183. UNLOCK_HTC_TX(target);
  184. netbuf = qdf_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. qdf_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. qdf_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 = qdf_nbuf_data(netbuf);
  208. pPacket->BufferLength = qdf_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 = qdf_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. qdf_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 = qdf_nbuf_headroom(netbuf);
  243. qdf_nbuf_pull_head(netbuf,
  244. pPacket->netbufOrigHeadRoom - curentHeadRoom);
  245. qdf_nbuf_trim_tail(netbuf, qdf_nbuf_len(netbuf));
  246. /* restore the pBuffer pointer. HIF changes this */
  247. pPacket->pBuffer = qdf_nbuf_data(netbuf);
  248. pPacket->BufferLength = qdf_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. qdf_nbuf_t bundleBuf;
  273. uint32_t data_attr = 0;
  274. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  275. data_len = pBundleBuffer - qdf_nbuf_data(bundleBuf);
  276. qdf_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. qdf_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 = qdf_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 = qdf_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 *) qdf_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 = qdf_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 = qdf_nbuf_get_frag_len(netbuf, i);
  385. unsigned char *frag_addr =
  386. qdf_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 != qdf_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. qdf_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 = (HTC_FRAME_HDR *)
  448. qdf_nbuf_get_frag_vaddr(netbuf, 0);
  449. AR_DEBUG_ASSERT(pHtcHdr);
  450. HTC_WRITE32(pHtcHdr,
  451. SM(payloadLen,
  452. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->
  453. PktInfo.
  454. AsTx.
  455. SendFlags,
  456. HTC_FRAME_HDR_FLAGS)
  457. | SM(pPacket->Endpoint,
  458. HTC_FRAME_HDR_ENDPOINTID));
  459. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  460. SM(pPacket->PktInfo.AsTx.SeqNo,
  461. HTC_FRAME_HDR_CONTROLBYTES1));
  462. /*
  463. * Now that the HTC frame header has been added, the netbuf can be
  464. * mapped. This only applies to non-data frames, since data frames
  465. * were already mapped as they entered into the driver.
  466. * Check the "FIXUP_NETBUF" flag to see whether this is a data netbuf
  467. * that is already mapped, or a non-data netbuf that needs to be
  468. * mapped.
  469. */
  470. if (pPacket->PktInfo.AsTx.
  471. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) {
  472. qdf_nbuf_map(target->osdev,
  473. GET_HTC_PACKET_NET_BUF_CONTEXT
  474. (pPacket), QDF_DMA_TO_DEVICE);
  475. }
  476. }
  477. LOCK_HTC_TX(target);
  478. /* store in look up queue to match completions */
  479. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  480. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  481. pEndpoint->ul_outstanding_cnt++;
  482. UNLOCK_HTC_TX(target);
  483. hif_send_complete_check(target->hif_dev, pEndpoint->UL_PipeID, false);
  484. status = hif_send_head(target->hif_dev,
  485. pEndpoint->UL_PipeID, pEndpoint->Id,
  486. HTC_HDR_LENGTH + pPacket->ActualLength,
  487. netbuf, data_attr);
  488. #if DEBUG_BUNDLE
  489. qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.\n",
  490. pEndpoint->Id,
  491. pEndpoint->TxCreditSize,
  492. HTC_HDR_LENGTH + pPacket->ActualLength);
  493. #endif
  494. target->ce_send_cnt++;
  495. if (qdf_unlikely(A_FAILED(status))) {
  496. if (status != A_NO_RESOURCE) {
  497. /* TODO : if more than 1 endpoint maps to the same PipeID it is possible
  498. * to run out of resources in the HIF layer. Don't emit the error */
  499. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  500. ("hif_send Failed status:%d \n",
  501. status));
  502. }
  503. LOCK_HTC_TX(target);
  504. target->ce_send_cnt--;
  505. pEndpoint->ul_outstanding_cnt--;
  506. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  507. /* reclaim credits */
  508. #if defined(HIF_USB)
  509. if (pEndpoint->Id >= ENDPOINT_2
  510. && pEndpoint->Id <= ENDPOINT_5)
  511. target->avail_tx_credits +=
  512. pPacket->PktInfo.AsTx.CreditsUsed;
  513. else
  514. pEndpoint->TxCredits +=
  515. pPacket->PktInfo.AsTx.CreditsUsed;
  516. #else
  517. pEndpoint->TxCredits +=
  518. pPacket->PktInfo.AsTx.CreditsUsed;
  519. #endif
  520. /* put it back into the callers queue */
  521. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  522. UNLOCK_HTC_TX(target);
  523. break;
  524. }
  525. /*
  526. * For HTT messages without a response from fw,
  527. * do the runtime put here.
  528. * otherwise runtime put will be done when the fw response comes
  529. */
  530. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_RUNTIME_PUT)
  531. hif_pm_runtime_put(target->hif_dev);
  532. }
  533. if (qdf_unlikely(A_FAILED(status))) {
  534. #if defined(HIF_USB)
  535. if (pEndpoint->Id >= ENDPOINT_2 && pEndpoint->Id <= ENDPOINT_5)
  536. target->avail_tx_credits +=
  537. pPacket->PktInfo.AsTx.CreditsUsed;
  538. else
  539. pEndpoint->TxCredits +=
  540. pPacket->PktInfo.AsTx.CreditsUsed;
  541. #endif
  542. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  543. ("htc_issue_packets, failed pkt:0x%p status:%d",
  544. pPacket, status));
  545. }
  546. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_issue_packets \n"));
  547. return status;
  548. }
  549. #ifdef FEATURE_RUNTIME_PM
  550. /**
  551. * extract_htc_pm_packtes(): move pm packets from endpoint into queue
  552. * @endpoint: which enpoint to extract packets from
  553. * @queue: a queue to store extracted packets in.
  554. *
  555. * remove pm packets from the endpoint's tx queue.
  556. * queue them into a queue
  557. */
  558. static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
  559. HTC_PACKET_QUEUE *queue)
  560. {
  561. HTC_PACKET *packet;
  562. /* only WMI endpoint has power management packets */
  563. if (endpoint->service_id != WMI_CONTROL_SVC)
  564. return;
  565. ITERATE_OVER_LIST_ALLOW_REMOVE(&endpoint->TxQueue.QueueHead, packet,
  566. HTC_PACKET, ListLink) {
  567. if (packet->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_AUTO_PM) {
  568. HTC_PACKET_REMOVE(&endpoint->TxQueue, packet);
  569. HTC_PACKET_ENQUEUE(queue, packet);
  570. }
  571. } ITERATE_END
  572. }
  573. /**
  574. * queue_htc_pm_packets(): queue pm packets with priority
  575. * @endpoint: enpoint to queue packets to
  576. * @queue: queue of pm packets to enque
  577. *
  578. * suspend resume packets get special treatment & priority.
  579. * need to queue them at the front of the queue.
  580. */
  581. static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
  582. HTC_PACKET_QUEUE *queue)
  583. {
  584. if (endpoint->service_id != WMI_CONTROL_SVC)
  585. return;
  586. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&endpoint->TxQueue, queue);
  587. }
  588. #else
  589. static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
  590. HTC_PACKET_QUEUE *queue)
  591. {}
  592. static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
  593. HTC_PACKET_QUEUE *queue)
  594. {}
  595. #endif
  596. /* get HTC send packets from the TX queue on an endpoint, based on available credits */
  597. void get_htc_send_packets_credit_based(HTC_TARGET *target,
  598. HTC_ENDPOINT *pEndpoint,
  599. HTC_PACKET_QUEUE *pQueue)
  600. {
  601. int creditsRequired;
  602. int remainder;
  603. A_UINT8 sendFlags;
  604. HTC_PACKET *pPacket;
  605. unsigned int transferLength;
  606. HTC_PACKET_QUEUE *tx_queue;
  607. HTC_PACKET_QUEUE pm_queue;
  608. bool do_pm_get = false;
  609. /****** NOTE : the TX lock is held when this function is called *****************/
  610. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+get_htc_send_packets_credit_based\n"));
  611. INIT_HTC_PACKET_QUEUE(&pm_queue);
  612. extract_htc_pm_packets(pEndpoint, &pm_queue);
  613. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  614. tx_queue = &pEndpoint->TxQueue;
  615. do_pm_get = true;
  616. } else {
  617. tx_queue = &pm_queue;
  618. }
  619. /* loop until we can grab as many packets out of the queue as we can */
  620. while (true) {
  621. if (do_pm_get && hif_pm_runtime_get(target->hif_dev)) {
  622. /* bus suspended, runtime resume issued */
  623. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  624. break;
  625. }
  626. sendFlags = 0;
  627. /* get packet at head, but don't remove it */
  628. pPacket = htc_get_pkt_at_head(tx_queue);
  629. if (pPacket == NULL) {
  630. if (do_pm_get)
  631. hif_pm_runtime_put(target->hif_dev);
  632. break;
  633. }
  634. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  635. (" Got head packet:%p , Queue Depth: %d\n",
  636. pPacket,
  637. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  638. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  639. if (transferLength <= pEndpoint->TxCreditSize) {
  640. creditsRequired = 1;
  641. } else {
  642. /* figure out how many credits this message requires */
  643. creditsRequired =
  644. transferLength / pEndpoint->TxCreditSize;
  645. remainder = transferLength % pEndpoint->TxCreditSize;
  646. if (remainder) {
  647. creditsRequired++;
  648. }
  649. }
  650. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  651. (" Credits Required:%d Got:%d\n",
  652. creditsRequired, pEndpoint->TxCredits));
  653. if (pEndpoint->Id == ENDPOINT_0) {
  654. /* endpoint 0 is special, it always has a credit and does not require credit based
  655. * flow control */
  656. creditsRequired = 0;
  657. #if defined(HIF_USB)
  658. } else if (pEndpoint->Id >= ENDPOINT_2
  659. && pEndpoint->Id <= ENDPOINT_5) {
  660. if (target->avail_tx_credits < creditsRequired)
  661. break;
  662. target->avail_tx_credits -= creditsRequired;
  663. if (target->avail_tx_credits < 9) {
  664. /* tell the target we need credits ASAP! */
  665. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  666. INC_HTC_EP_STAT(pEndpoint,
  667. TxCreditLowIndications, 1);
  668. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  669. (" Host Needs Credits \n"));
  670. }
  671. #endif
  672. } else {
  673. if (pEndpoint->TxCredits < creditsRequired) {
  674. #if DEBUG_CREDIT
  675. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  676. (" EP%d, No Credit now. %d < %d\n",
  677. pEndpoint->Id,
  678. pEndpoint->TxCredits,
  679. creditsRequired));
  680. #endif
  681. if (do_pm_get)
  682. hif_pm_runtime_put(target->hif_dev);
  683. break;
  684. }
  685. pEndpoint->TxCredits -= creditsRequired;
  686. INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
  687. creditsRequired);
  688. /* check if we need credits back from the target */
  689. if (pEndpoint->TxCredits <=
  690. pEndpoint->TxCreditsPerMaxMsg) {
  691. /* tell the target we need credits ASAP! */
  692. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  693. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  694. LOCK_HTC_CREDIT(target);
  695. htc_credit_record(HTC_REQUEST_CREDIT,
  696. pEndpoint->TxCredits,
  697. HTC_PACKET_QUEUE_DEPTH
  698. (tx_queue));
  699. UNLOCK_HTC_CREDIT(target);
  700. }
  701. INC_HTC_EP_STAT(pEndpoint,
  702. TxCreditLowIndications, 1);
  703. #if DEBUG_CREDIT
  704. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  705. (" EP%d Needs Credits\n",
  706. pEndpoint->Id));
  707. #endif
  708. }
  709. }
  710. /* now we can fully dequeue */
  711. pPacket = htc_packet_dequeue(tx_queue);
  712. if (pPacket) {
  713. /* save the number of credits this packet consumed */
  714. pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
  715. /* save send flags */
  716. pPacket->PktInfo.AsTx.SendFlags = sendFlags;
  717. /* queue this packet into the caller's queue */
  718. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  719. }
  720. }
  721. if (!HTC_QUEUE_EMPTY(&pm_queue))
  722. queue_htc_pm_packets(pEndpoint, &pm_queue);
  723. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  724. ("-get_htc_send_packets_credit_based\n"));
  725. }
  726. void get_htc_send_packets(HTC_TARGET *target,
  727. HTC_ENDPOINT *pEndpoint,
  728. HTC_PACKET_QUEUE *pQueue, int Resources)
  729. {
  730. HTC_PACKET *pPacket;
  731. HTC_PACKET_QUEUE *tx_queue;
  732. HTC_PACKET_QUEUE pm_queue;
  733. bool do_pm_get;
  734. /****** NOTE : the TX lock is held when this function is called *****************/
  735. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  736. ("+get_htc_send_packets %d resources\n", Resources));
  737. INIT_HTC_PACKET_QUEUE(&pm_queue);
  738. extract_htc_pm_packets(pEndpoint, &pm_queue);
  739. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  740. tx_queue = &pEndpoint->TxQueue;
  741. do_pm_get = true;
  742. } else {
  743. tx_queue = &pm_queue;
  744. }
  745. /* loop until we can grab as many packets out of the queue as we can */
  746. while (Resources > 0) {
  747. int num_frags;
  748. if (do_pm_get && hif_pm_runtime_get(target->hif_dev)) {
  749. /* bus suspended, runtime resume issued */
  750. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  751. break;
  752. }
  753. pPacket = htc_packet_dequeue(tx_queue);
  754. if (pPacket == NULL) {
  755. if (do_pm_get)
  756. hif_pm_runtime_put(target->hif_dev);
  757. break;
  758. }
  759. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  760. (" Got packet:%p , New Queue Depth: %d\n",
  761. pPacket,
  762. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  763. /* For non-credit path the sequence number is already embedded
  764. * in the constructed HTC header
  765. */
  766. #if 0
  767. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  768. pEndpoint->SeqNo++;
  769. #endif
  770. pPacket->PktInfo.AsTx.SendFlags = 0;
  771. pPacket->PktInfo.AsTx.CreditsUsed = 0;
  772. /* queue this packet into the caller's queue */
  773. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  774. /*
  775. * FIX THIS:
  776. * For now, avoid calling qdf_nbuf_get_num_frags before calling
  777. * qdf_nbuf_map, because the MacOS version of qdf_nbuf_t doesn't
  778. * support qdf_nbuf_get_num_frags until after qdf_nbuf_map has
  779. * been done.
  780. * Assume that the non-data netbufs, i.e. the WMI message netbufs,
  781. * consist of a single fragment.
  782. */
  783. num_frags =
  784. (pPacket->PktInfo.AsTx.
  785. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1
  786. /* WMI messages are in a single-fragment network buffer */ :
  787. qdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
  788. (pPacket));
  789. Resources -= num_frags;
  790. }
  791. if (!HTC_QUEUE_EMPTY(&pm_queue))
  792. queue_htc_pm_packets(pEndpoint, &pm_queue);
  793. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets\n"));
  794. }
  795. static HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
  796. HTC_ENDPOINT *pEndpoint,
  797. HTC_PACKET_QUEUE *pCallersSendQueue)
  798. {
  799. HTC_PACKET_QUEUE sendQueue; /* temp queue to hold packets at various stages */
  800. HTC_PACKET *pPacket;
  801. int tx_resources;
  802. int overflow;
  803. HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
  804. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%p Depth:%d)\n",
  805. pCallersSendQueue,
  806. (pCallersSendQueue ==
  807. NULL) ? 0 :
  808. HTC_PACKET_QUEUE_DEPTH
  809. (pCallersSendQueue)));
  810. /* init the local send queue */
  811. INIT_HTC_PACKET_QUEUE(&sendQueue);
  812. do {
  813. if (NULL == pCallersSendQueue) {
  814. /* caller didn't provide a queue, just wants us to check queues and send */
  815. break;
  816. }
  817. if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
  818. /* empty queue */
  819. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  820. HTC_PKT_Q_EMPTY);
  821. result = HTC_SEND_QUEUE_DROP;
  822. break;
  823. }
  824. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
  825. pEndpoint->MaxTxQueueDepth) {
  826. /* we've already overflowed */
  827. overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  828. } else {
  829. /* figure out how much we will overflow by */
  830. overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  831. overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  832. /* figure out how much we will overflow the TX queue by */
  833. overflow -= pEndpoint->MaxTxQueueDepth;
  834. }
  835. /* if overflow is negative or zero, we are okay */
  836. if (overflow > 0) {
  837. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  838. (" Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d \n",
  839. pEndpoint->Id, overflow,
  840. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  841. TxQueue),
  842. pEndpoint->MaxTxQueueDepth));
  843. }
  844. if ((overflow <= 0)
  845. || (pEndpoint->EpCallBacks.EpSendFull == NULL)) {
  846. /* all packets will fit or caller did not provide send full indication handler
  847. * -- just move all of them to the local sendQueue object */
  848. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
  849. pCallersSendQueue);
  850. } else {
  851. int i;
  852. int goodPkts =
  853. HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
  854. overflow;
  855. A_ASSERT(goodPkts >= 0);
  856. /* we have overflowed, and a callback is provided */
  857. /* dequeue all non-overflow packets into the sendqueue */
  858. for (i = 0; i < goodPkts; i++) {
  859. /* pop off caller's queue */
  860. pPacket = htc_packet_dequeue(pCallersSendQueue);
  861. A_ASSERT(pPacket != NULL);
  862. /* insert into local queue */
  863. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  864. }
  865. /* the caller's queue has all the packets that won't fit */
  866. /* walk through the caller's queue and indicate each one to the send full handler */
  867. ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
  868. QueueHead, pPacket,
  869. HTC_PACKET, ListLink) {
  870. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  871. (" Indicating overflowed TX packet: %p \n",
  872. pPacket));
  873. /*
  874. * Remove headroom reserved for HTC_FRAME_HDR before giving
  875. * the packet back to the user via the EpSendFull callback.
  876. */
  877. restore_tx_packet(target, pPacket);
  878. if (pEndpoint->EpCallBacks.
  879. EpSendFull(pEndpoint->EpCallBacks.pContext,
  880. pPacket) == HTC_SEND_FULL_DROP) {
  881. /* callback wants the packet dropped */
  882. INC_HTC_EP_STAT(pEndpoint, TxDropped,
  883. 1);
  884. /* leave this one in the caller's queue for cleanup */
  885. } else {
  886. /* callback wants to keep this packet, remove from caller's queue */
  887. HTC_PACKET_REMOVE(pCallersSendQueue,
  888. pPacket);
  889. /* put it in the send queue */
  890. /* add HTC_FRAME_HDR space reservation again */
  891. qdf_nbuf_push_head
  892. (GET_HTC_PACKET_NET_BUF_CONTEXT
  893. (pPacket), sizeof(HTC_FRAME_HDR));
  894. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  895. }
  896. }
  897. ITERATE_END;
  898. if (HTC_QUEUE_EMPTY(&sendQueue)) {
  899. /* no packets made it in, caller will cleanup */
  900. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  901. HTC_SEND_Q_EMPTY);
  902. result = HTC_SEND_QUEUE_DROP;
  903. break;
  904. }
  905. }
  906. } while (false);
  907. if (result != HTC_SEND_QUEUE_OK) {
  908. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: \n"));
  909. return result;
  910. }
  911. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  912. tx_resources =
  913. hif_get_free_queue_number(target->hif_dev,
  914. pEndpoint->UL_PipeID);
  915. } else {
  916. tx_resources = 0;
  917. }
  918. LOCK_HTC_TX(target);
  919. if (!HTC_QUEUE_EMPTY(&sendQueue)) {
  920. /* transfer packets to tail */
  921. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
  922. &sendQueue);
  923. A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
  924. INIT_HTC_PACKET_QUEUE(&sendQueue);
  925. }
  926. /* increment tx processing count on entry */
  927. qdf_atomic_inc(&pEndpoint->TxProcessCount);
  928. if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  929. /* another thread or task is draining the TX queues on this endpoint
  930. * that thread will reset the tx processing count when the queue is drained */
  931. qdf_atomic_dec(&pEndpoint->TxProcessCount);
  932. UNLOCK_HTC_TX(target);
  933. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy) \n"));
  934. return HTC_SEND_QUEUE_OK;
  935. }
  936. /***** beyond this point only 1 thread may enter ******/
  937. /* now drain the endpoint TX queue for transmission as long as we have enough
  938. * transmit resources */
  939. while (true) {
  940. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0) {
  941. break;
  942. }
  943. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  944. #if DEBUG_CREDIT
  945. int cred = pEndpoint->TxCredits;
  946. #endif
  947. /* credit based mechanism provides flow control based on target transmit resource availability, we
  948. * assume that the HIF layer will always have bus resources greater than target transmit resources */
  949. get_htc_send_packets_credit_based(target, pEndpoint,
  950. &sendQueue);
  951. #if DEBUG_CREDIT
  952. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  953. if (cred - pEndpoint->TxCredits > 0) {
  954. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  955. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  956. pEndpoint->Id, cred,
  957. cred -
  958. pEndpoint->TxCredits,
  959. pEndpoint->TxCredits));
  960. }
  961. }
  962. #endif
  963. } else {
  964. /* get all the packets for this endpoint that we can for this pass */
  965. get_htc_send_packets(target, pEndpoint, &sendQueue,
  966. tx_resources);
  967. }
  968. if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
  969. /* didn't get any packets due to a lack of resources or TX queue was drained */
  970. break;
  971. }
  972. UNLOCK_HTC_TX(target);
  973. /* send what we can */
  974. result = htc_issue_packets(target, pEndpoint, &sendQueue);
  975. if (result) {
  976. int i;
  977. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  978. ("htc_issue_packets, failed status:%d put it back to head of callersSendQueue",
  979. result));
  980. for (i = HTC_PACKET_QUEUE_DEPTH(&sendQueue); i > 0; i--)
  981. hif_pm_runtime_put(target->hif_dev);
  982. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  983. &sendQueue);
  984. LOCK_HTC_TX(target);
  985. break;
  986. }
  987. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  988. tx_resources =
  989. hif_get_free_queue_number(target->hif_dev,
  990. pEndpoint->UL_PipeID);
  991. }
  992. LOCK_HTC_TX(target);
  993. }
  994. UNLOCK_HTC_TX(target);
  995. /* done with this endpoint, we can clear the count */
  996. qdf_atomic_init(&pEndpoint->TxProcessCount);
  997. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: \n"));
  998. return HTC_SEND_QUEUE_OK;
  999. }
  1000. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1001. static A_UINT16 htc_send_pkts_sched_check(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID id)
  1002. {
  1003. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1004. HTC_ENDPOINT *pEndpoint;
  1005. HTC_ENDPOINT_ID eid;
  1006. HTC_PACKET_QUEUE *pTxQueue;
  1007. A_UINT16 resources;
  1008. A_UINT16 acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
  1009. if (id < ENDPOINT_2 || id > ENDPOINT_5) {
  1010. return 1;
  1011. }
  1012. for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
  1013. pEndpoint = &target->endpoint[eid];
  1014. pTxQueue = &pEndpoint->TxQueue;
  1015. if (HTC_QUEUE_EMPTY(pTxQueue)) {
  1016. acQueueStatus[eid - 2] = 1;
  1017. }
  1018. }
  1019. switch (id) {
  1020. case ENDPOINT_2: /* BE */
  1021. return acQueueStatus[0] && acQueueStatus[2]
  1022. && acQueueStatus[3];
  1023. case ENDPOINT_3: /* BK */
  1024. return acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
  1025. && acQueueStatus[3];
  1026. case ENDPOINT_4: /* VI */
  1027. return acQueueStatus[2] && acQueueStatus[3];
  1028. case ENDPOINT_5: /* VO */
  1029. return acQueueStatus[3];
  1030. default:
  1031. return 0;
  1032. }
  1033. }
  1034. static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
  1035. HTC_PACKET_QUEUE *pPktQueue,
  1036. HTC_ENDPOINT_ID eid)
  1037. {
  1038. HTC_ENDPOINT *pEndpoint;
  1039. HTC_PACKET_QUEUE *pTxQueue;
  1040. HTC_PACKET *pPacket;
  1041. int goodPkts;
  1042. pEndpoint = &target->endpoint[eid];
  1043. pTxQueue = &pEndpoint->TxQueue;
  1044. LOCK_HTC_TX(target);
  1045. goodPkts =
  1046. pEndpoint->MaxTxQueueDepth -
  1047. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  1048. if (goodPkts > 0) {
  1049. while (!HTC_QUEUE_EMPTY(pPktQueue)) {
  1050. pPacket = htc_packet_dequeue(pPktQueue);
  1051. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  1052. goodPkts--;
  1053. if (goodPkts <= 0) {
  1054. break;
  1055. }
  1056. }
  1057. }
  1058. if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
  1059. ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
  1060. HTC_PACKET, ListLink) {
  1061. if (pEndpoint->EpCallBacks.
  1062. EpSendFull(pEndpoint->EpCallBacks.pContext,
  1063. pPacket) == HTC_SEND_FULL_DROP) {
  1064. INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
  1065. } else {
  1066. HTC_PACKET_REMOVE(pPktQueue, pPacket);
  1067. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  1068. }
  1069. }
  1070. ITERATE_END;
  1071. }
  1072. UNLOCK_HTC_TX(target);
  1073. return A_OK;
  1074. }
  1075. #endif
  1076. A_STATUS htc_send_pkts_multiple(HTC_HANDLE HTCHandle, HTC_PACKET_QUEUE *pPktQueue)
  1077. {
  1078. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1079. HTC_ENDPOINT *pEndpoint;
  1080. HTC_PACKET *pPacket;
  1081. qdf_nbuf_t netbuf;
  1082. HTC_FRAME_HDR *pHtcHdr;
  1083. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1084. ("+htc_send_pkts_multiple: Queue: %p, Pkts %d \n",
  1085. pPktQueue, HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
  1086. /* get packet at head to figure out which endpoint these packets will go into */
  1087. pPacket = htc_get_pkt_at_head(pPktQueue);
  1088. if (NULL == pPacket) {
  1089. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
  1090. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_send_pkts_multiple \n"));
  1091. return A_EINVAL;
  1092. }
  1093. AR_DEBUG_ASSERT(pPacket->Endpoint < ENDPOINT_MAX);
  1094. pEndpoint = &target->endpoint[pPacket->Endpoint];
  1095. if (!pEndpoint->service_id) {
  1096. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
  1097. __func__));
  1098. return A_EINVAL;
  1099. }
  1100. #ifdef HTC_EP_STAT_PROFILING
  1101. LOCK_HTC_TX(target);
  1102. INC_HTC_EP_STAT(pEndpoint, TxPosted, HTC_PACKET_QUEUE_DEPTH(pPktQueue));
  1103. UNLOCK_HTC_TX(target);
  1104. #endif
  1105. /* provide room in each packet's netbuf for the HTC frame header */
  1106. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue, pPacket) {
  1107. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1108. AR_DEBUG_ASSERT(netbuf);
  1109. qdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
  1110. /* setup HTC frame header */
  1111. pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1112. AR_DEBUG_ASSERT(pHtcHdr);
  1113. HTC_WRITE32(pHtcHdr,
  1114. SM(pPacket->ActualLength,
  1115. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->Endpoint,
  1116. HTC_FRAME_HDR_ENDPOINTID));
  1117. LOCK_HTC_TX(target);
  1118. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1119. pEndpoint->SeqNo++;
  1120. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1121. SM(pPacket->PktInfo.AsTx.SeqNo,
  1122. HTC_FRAME_HDR_CONTROLBYTES1));
  1123. UNLOCK_HTC_TX(target);
  1124. /*
  1125. * Now that the HTC frame header has been added, the netbuf can be
  1126. * mapped. This only applies to non-data frames, since data frames
  1127. * were already mapped as they entered into the driver.
  1128. */
  1129. qdf_nbuf_map(target->osdev,
  1130. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  1131. QDF_DMA_TO_DEVICE);
  1132. pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  1133. }
  1134. HTC_PACKET_QUEUE_ITERATE_END;
  1135. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1136. if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id)) {
  1137. htc_send_pkts_sched_queue(HTCHandle, pPktQueue, pEndpoint->Id);
  1138. } else {
  1139. htc_try_send(target, pEndpoint, pPktQueue);
  1140. }
  1141. #else
  1142. htc_try_send(target, pEndpoint, pPktQueue);
  1143. #endif
  1144. /* do completion on any packets that couldn't get in */
  1145. if (!HTC_QUEUE_EMPTY(pPktQueue)) {
  1146. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue, pPacket) {
  1147. /* remove the headroom reserved for HTC_FRAME_HDR */
  1148. restore_tx_packet(target, pPacket);
  1149. if (HTC_STOPPING(target)) {
  1150. pPacket->Status = A_ECANCELED;
  1151. } else {
  1152. pPacket->Status = A_NO_RESOURCE;
  1153. }
  1154. }
  1155. HTC_PACKET_QUEUE_ITERATE_END;
  1156. do_send_completion(pEndpoint, pPktQueue);
  1157. }
  1158. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_send_pkts_multiple \n"));
  1159. return A_OK;
  1160. }
  1161. /* HTC API - htc_send_pkt */
  1162. A_STATUS htc_send_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket)
  1163. {
  1164. HTC_PACKET_QUEUE queue;
  1165. if (HTCHandle == NULL || pPacket == NULL) {
  1166. return A_ERROR;
  1167. }
  1168. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1169. ("+-htc_send_pkt: Enter endPointId: %d, buffer: %p, length: %d \n",
  1170. pPacket->Endpoint, pPacket->pBuffer,
  1171. pPacket->ActualLength));
  1172. INIT_HTC_PACKET_QUEUE_AND_ADD(&queue, pPacket);
  1173. return htc_send_pkts_multiple(HTCHandle, &queue);
  1174. }
  1175. #ifdef ATH_11AC_TXCOMPACT
  1176. A_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, qdf_nbuf_t netbuf, int Epid,
  1177. int ActualLength)
  1178. {
  1179. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1180. HTC_ENDPOINT *pEndpoint;
  1181. HTC_FRAME_HDR *pHtcHdr;
  1182. A_STATUS status = A_OK;
  1183. int tx_resources;
  1184. uint32_t data_attr = 0;
  1185. pEndpoint = &target->endpoint[Epid];
  1186. tx_resources =
  1187. hif_get_free_queue_number(target->hif_dev, pEndpoint->UL_PipeID);
  1188. if (tx_resources < HTC_DATA_RESOURCE_THRS) {
  1189. if (pEndpoint->ul_is_polled) {
  1190. hif_send_complete_check(pEndpoint->target->hif_dev,
  1191. pEndpoint->UL_PipeID, 1);
  1192. tx_resources =
  1193. hif_get_free_queue_number(target->hif_dev,
  1194. pEndpoint->UL_PipeID);
  1195. }
  1196. if (tx_resources < HTC_DATA_MINDESC_PERPACKET) {
  1197. return A_ERROR;
  1198. }
  1199. }
  1200. if (hif_pm_runtime_get(target->hif_dev))
  1201. return A_ERROR;
  1202. pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1203. AR_DEBUG_ASSERT(pHtcHdr);
  1204. data_attr = qdf_nbuf_data_attr_get(netbuf);
  1205. HTC_WRITE32(pHtcHdr, SM(ActualLength, HTC_FRAME_HDR_PAYLOADLEN) |
  1206. SM(Epid, HTC_FRAME_HDR_ENDPOINTID));
  1207. /*
  1208. * If the HIF pipe for the data endpoint is polled rather than
  1209. * interrupt-driven, this is a good point to check whether any
  1210. * data previously sent through the HIF pipe have finished being
  1211. * sent.
  1212. * Since this may result in callbacks to htc_tx_completion_handler,
  1213. * which can take the HTC tx lock, make the hif_send_complete_check
  1214. * call before acquiring the HTC tx lock.
  1215. * Call hif_send_complete_check directly, rather than calling
  1216. * htc_send_complete_check, and call the PollTimerStart separately
  1217. * after calling hif_send_head, so the timer will be started to
  1218. * check for completion of the new outstanding download (in the
  1219. * unexpected event that other polling calls don't catch it).
  1220. */
  1221. LOCK_HTC_TX(target);
  1222. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1223. SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
  1224. pEndpoint->SeqNo++;
  1225. QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
  1226. DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1227. (uint8_t *)(qdf_nbuf_data(netbuf)),
  1228. sizeof(qdf_nbuf_data(netbuf))));
  1229. status = hif_send_head(target->hif_dev,
  1230. pEndpoint->UL_PipeID,
  1231. pEndpoint->Id, ActualLength, netbuf, data_attr);
  1232. UNLOCK_HTC_TX(target);
  1233. return status;
  1234. }
  1235. #else /*ATH_11AC_TXCOMPACT */
  1236. A_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
  1237. A_UINT8 more_data)
  1238. {
  1239. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1240. HTC_ENDPOINT *pEndpoint;
  1241. HTC_FRAME_HDR *pHtcHdr;
  1242. HTC_PACKET_QUEUE sendQueue;
  1243. qdf_nbuf_t netbuf = NULL;
  1244. int tx_resources;
  1245. A_STATUS status = A_OK;
  1246. uint32_t data_attr = 0;
  1247. if (pPacket) {
  1248. AR_DEBUG_ASSERT(pPacket->Endpoint < ENDPOINT_MAX);
  1249. pEndpoint = &target->endpoint[pPacket->Endpoint];
  1250. /* add HTC_FRAME_HDR in the initial fragment */
  1251. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1252. pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
  1253. AR_DEBUG_ASSERT(pHtcHdr);
  1254. HTC_WRITE32(pHtcHdr,
  1255. SM(pPacket->ActualLength,
  1256. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->PktInfo.
  1257. AsTx.SendFlags,
  1258. HTC_FRAME_HDR_FLAGS)
  1259. | SM(pPacket->Endpoint, HTC_FRAME_HDR_ENDPOINTID));
  1260. /*
  1261. * If the HIF pipe for the data endpoint is polled rather than
  1262. * interrupt-driven, this is a good point to check whether any
  1263. * data previously sent through the HIF pipe have finished being
  1264. * sent.
  1265. * Since this may result in callbacks to htc_tx_completion_handler,
  1266. * which can take the HTC tx lock, make the hif_send_complete_check
  1267. * call before acquiring the HTC tx lock.
  1268. * Call hif_send_complete_check directly, rather than calling
  1269. * htc_send_complete_check, and call the PollTimerStart separately
  1270. * after calling hif_send_head, so the timer will be started to
  1271. * check for completion of the new outstanding download (in the
  1272. * unexpected event that other polling calls don't catch it).
  1273. */
  1274. if (pEndpoint->ul_is_polled) {
  1275. htc_send_complete_poll_timer_stop(pEndpoint);
  1276. hif_send_complete_check(pEndpoint->target->hif_dev,
  1277. pEndpoint->UL_PipeID, 0);
  1278. }
  1279. LOCK_HTC_TX(target);
  1280. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1281. pEndpoint->SeqNo++;
  1282. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1283. SM(pPacket->PktInfo.AsTx.SeqNo,
  1284. HTC_FRAME_HDR_CONTROLBYTES1));
  1285. /* append new packet to pEndpoint->TxQueue */
  1286. HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
  1287. #ifdef ENABLE_BUNDLE_TX
  1288. if (HTC_ENABLE_BUNDLE(target) && (more_data)) {
  1289. UNLOCK_HTC_TX(target);
  1290. return A_OK;
  1291. }
  1292. #endif
  1293. } else {
  1294. LOCK_HTC_TX(target);
  1295. pEndpoint = &target->endpoint[1];
  1296. }
  1297. /* increment tx processing count on entry */
  1298. qdf_atomic_inc(&pEndpoint->TxProcessCount);
  1299. if (qdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  1300. /*
  1301. * Another thread or task is draining the TX queues on this endpoint.
  1302. * That thread will reset the tx processing count when the queue is
  1303. * drained.
  1304. */
  1305. qdf_atomic_dec(&pEndpoint->TxProcessCount);
  1306. UNLOCK_HTC_TX(target);
  1307. return A_OK;
  1308. }
  1309. /***** beyond this point only 1 thread may enter ******/
  1310. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1311. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1312. #if DEBUG_CREDIT
  1313. int cred = pEndpoint->TxCredits;
  1314. #endif
  1315. get_htc_send_packets_credit_based(target, pEndpoint, &sendQueue);
  1316. #if DEBUG_CREDIT
  1317. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1318. if (cred - pEndpoint->TxCredits > 0) {
  1319. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1320. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  1321. pEndpoint->Id, cred,
  1322. cred - pEndpoint->TxCredits,
  1323. pEndpoint->TxCredits));
  1324. }
  1325. }
  1326. #endif
  1327. UNLOCK_HTC_TX(target);
  1328. }
  1329. #ifdef ENABLE_BUNDLE_TX
  1330. else if (HTC_ENABLE_BUNDLE(target)) {
  1331. /* Dequeue max packets from endpoint tx queue */
  1332. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1333. HTC_MAX_TX_BUNDLE_SEND_LIMIT);
  1334. UNLOCK_HTC_TX(target);
  1335. }
  1336. #endif
  1337. else {
  1338. /*
  1339. * Now drain the endpoint TX queue for transmission as long as we have
  1340. * enough transmit resources
  1341. */
  1342. tx_resources =
  1343. hif_get_free_queue_number(target->hif_dev,
  1344. pEndpoint->UL_PipeID);
  1345. get_htc_send_packets(target, pEndpoint, &sendQueue, tx_resources);
  1346. UNLOCK_HTC_TX(target);
  1347. }
  1348. QDF_NBUF_UPDATE_TX_PKT_COUNT(netbuf, QDF_NBUF_TX_PKT_HTC);
  1349. DPTRACE(qdf_dp_trace(netbuf, QDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1350. (uint8_t *)(qdf_nbuf_data(netbuf)),
  1351. sizeof(qdf_nbuf_data(netbuf))));
  1352. /* send what we can */
  1353. while (true) {
  1354. #if defined(HIF_USB) || defined(HIF_SDIO)
  1355. #ifdef ENABLE_BUNDLE_TX
  1356. if (HTC_ENABLE_BUNDLE(target) &&
  1357. HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
  1358. HTC_MIN_MSG_PER_BUNDLE) {
  1359. htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
  1360. }
  1361. #endif
  1362. #endif
  1363. pPacket = htc_packet_dequeue(&sendQueue);
  1364. if (pPacket == NULL) {
  1365. break;
  1366. }
  1367. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1368. LOCK_HTC_TX(target);
  1369. /* store in look up queue to match completions */
  1370. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  1371. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  1372. pEndpoint->ul_outstanding_cnt++;
  1373. UNLOCK_HTC_TX(target);
  1374. status = hif_send_head(target->hif_dev,
  1375. pEndpoint->UL_PipeID,
  1376. pEndpoint->Id,
  1377. HTC_HDR_LENGTH + pPacket->ActualLength,
  1378. netbuf, data_attr);
  1379. #if DEBUG_BUNDLE
  1380. qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.\n",
  1381. pEndpoint->Id,
  1382. pEndpoint->TxCreditSize,
  1383. HTC_HDR_LENGTH + pPacket->ActualLength);
  1384. #endif
  1385. if (qdf_unlikely(A_FAILED(status))) {
  1386. LOCK_HTC_TX(target);
  1387. pEndpoint->ul_outstanding_cnt--;
  1388. /* remove this packet from the tx completion queue */
  1389. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  1390. /*
  1391. * Don't bother reclaiming credits - HTC flow control
  1392. * is not applicable to tx data.
  1393. * In LL systems, there is no download flow control,
  1394. * since there's virtually no download delay.
  1395. * In HL systems, the txrx SW explicitly performs the
  1396. * tx flow control.
  1397. */
  1398. /* pEndpoint->TxCredits += pPacket->PktInfo.AsTx.CreditsUsed; */
  1399. /* put this frame back at the front of the sendQueue */
  1400. HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
  1401. /* put the sendQueue back at the front of pEndpoint->TxQueue */
  1402. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1403. &sendQueue);
  1404. UNLOCK_HTC_TX(target);
  1405. break; /* still need to reset TxProcessCount */
  1406. }
  1407. }
  1408. /* done with this endpoint, we can clear the count */
  1409. qdf_atomic_init(&pEndpoint->TxProcessCount);
  1410. if (pEndpoint->ul_is_polled) {
  1411. /*
  1412. * Start a cleanup timer to poll for download completion.
  1413. * The download completion should be noticed promptly from
  1414. * other polling calls, but the timer provides a safety net
  1415. * in case other polling calls don't occur as expected.
  1416. */
  1417. htc_send_complete_poll_timer_start(pEndpoint);
  1418. }
  1419. return status;
  1420. }
  1421. #endif /*ATH_11AC_TXCOMPACT */
  1422. /*
  1423. * In the adapted HIF layer, qdf_nbuf_t are passed between HIF and HTC,
  1424. * since upper layers expects HTC_PACKET containers we use the completed netbuf
  1425. * and lookup its corresponding HTC packet buffer from a lookup list.
  1426. * This is extra overhead that can be fixed by re-aligning HIF interfaces
  1427. * with HTC.
  1428. *
  1429. */
  1430. static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
  1431. HTC_ENDPOINT *pEndpoint,
  1432. qdf_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 == (qdf_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. (qdf_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. qdf_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. }