htc_send.c 57 KB

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