htc_send.c 53 KB

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