htc.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482
  1. /*
  2. * Copyright (c) 2007-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "core.h"
  17. #include "hif.h"
  18. #include "debug.h"
  19. #include "hif-ops.h"
  20. #include <asm/unaligned.h>
  21. #define CALC_TXRX_PADDED_LEN(dev, len) (__ALIGN_MASK((len), (dev)->block_mask))
  22. static void ath6kl_htc_tx_buf_align(u8 **buf, unsigned long len)
  23. {
  24. u8 *align_addr;
  25. if (!IS_ALIGNED((unsigned long) *buf, 4)) {
  26. align_addr = PTR_ALIGN(*buf - 4, 4);
  27. memmove(align_addr, *buf, len);
  28. *buf = align_addr;
  29. }
  30. }
  31. static void ath6kl_htc_tx_prep_pkt(struct htc_packet *packet, u8 flags,
  32. int ctrl0, int ctrl1)
  33. {
  34. struct htc_frame_hdr *hdr;
  35. packet->buf -= HTC_HDR_LENGTH;
  36. hdr = (struct htc_frame_hdr *)packet->buf;
  37. /* Endianess? */
  38. put_unaligned((u16)packet->act_len, &hdr->payld_len);
  39. hdr->flags = flags;
  40. hdr->eid = packet->endpoint;
  41. hdr->ctrl[0] = ctrl0;
  42. hdr->ctrl[1] = ctrl1;
  43. }
  44. static void htc_reclaim_txctrl_buf(struct htc_target *target,
  45. struct htc_packet *pkt)
  46. {
  47. spin_lock_bh(&target->htc_lock);
  48. list_add_tail(&pkt->list, &target->free_ctrl_txbuf);
  49. spin_unlock_bh(&target->htc_lock);
  50. }
  51. static struct htc_packet *htc_get_control_buf(struct htc_target *target,
  52. bool tx)
  53. {
  54. struct htc_packet *packet = NULL;
  55. struct list_head *buf_list;
  56. buf_list = tx ? &target->free_ctrl_txbuf : &target->free_ctrl_rxbuf;
  57. spin_lock_bh(&target->htc_lock);
  58. if (list_empty(buf_list)) {
  59. spin_unlock_bh(&target->htc_lock);
  60. return NULL;
  61. }
  62. packet = list_first_entry(buf_list, struct htc_packet, list);
  63. list_del(&packet->list);
  64. spin_unlock_bh(&target->htc_lock);
  65. if (tx)
  66. packet->buf = packet->buf_start + HTC_HDR_LENGTH;
  67. return packet;
  68. }
  69. static void htc_tx_comp_update(struct htc_target *target,
  70. struct htc_endpoint *endpoint,
  71. struct htc_packet *packet)
  72. {
  73. packet->completion = NULL;
  74. packet->buf += HTC_HDR_LENGTH;
  75. if (!packet->status)
  76. return;
  77. ath6kl_err("req failed (status:%d, ep:%d, len:%d creds:%d)\n",
  78. packet->status, packet->endpoint, packet->act_len,
  79. packet->info.tx.cred_used);
  80. /* on failure to submit, reclaim credits for this packet */
  81. spin_lock_bh(&target->tx_lock);
  82. endpoint->cred_dist.cred_to_dist +=
  83. packet->info.tx.cred_used;
  84. endpoint->cred_dist.txq_depth = get_queue_depth(&endpoint->txq);
  85. ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx ctxt 0x%p dist 0x%p\n",
  86. target->cred_dist_cntxt, &target->cred_dist_list);
  87. ath6k_credit_distribute(target->cred_dist_cntxt,
  88. &target->cred_dist_list,
  89. HTC_CREDIT_DIST_SEND_COMPLETE);
  90. spin_unlock_bh(&target->tx_lock);
  91. }
  92. static void htc_tx_complete(struct htc_endpoint *endpoint,
  93. struct list_head *txq)
  94. {
  95. if (list_empty(txq))
  96. return;
  97. ath6kl_dbg(ATH6KL_DBG_HTC,
  98. "htc tx complete ep %d pkts %d\n",
  99. endpoint->eid, get_queue_depth(txq));
  100. ath6kl_tx_complete(endpoint->target->dev->ar, txq);
  101. }
  102. static void htc_tx_comp_handler(struct htc_target *target,
  103. struct htc_packet *packet)
  104. {
  105. struct htc_endpoint *endpoint = &target->endpoint[packet->endpoint];
  106. struct list_head container;
  107. htc_tx_comp_update(target, endpoint, packet);
  108. INIT_LIST_HEAD(&container);
  109. list_add_tail(&packet->list, &container);
  110. /* do completion */
  111. htc_tx_complete(endpoint, &container);
  112. }
  113. static void htc_async_tx_scat_complete(struct htc_target *target,
  114. struct hif_scatter_req *scat_req)
  115. {
  116. struct htc_endpoint *endpoint;
  117. struct htc_packet *packet;
  118. struct list_head tx_compq;
  119. int i;
  120. INIT_LIST_HEAD(&tx_compq);
  121. ath6kl_dbg(ATH6KL_DBG_HTC,
  122. "htc tx scat complete len %d entries %d\n",
  123. scat_req->len, scat_req->scat_entries);
  124. if (scat_req->status)
  125. ath6kl_err("send scatter req failed: %d\n", scat_req->status);
  126. packet = scat_req->scat_list[0].packet;
  127. endpoint = &target->endpoint[packet->endpoint];
  128. /* walk through the scatter list and process */
  129. for (i = 0; i < scat_req->scat_entries; i++) {
  130. packet = scat_req->scat_list[i].packet;
  131. if (!packet) {
  132. WARN_ON(1);
  133. return;
  134. }
  135. packet->status = scat_req->status;
  136. htc_tx_comp_update(target, endpoint, packet);
  137. list_add_tail(&packet->list, &tx_compq);
  138. }
  139. /* free scatter request */
  140. hif_scatter_req_add(target->dev->ar, scat_req);
  141. /* complete all packets */
  142. htc_tx_complete(endpoint, &tx_compq);
  143. }
  144. static int ath6kl_htc_tx_issue(struct htc_target *target,
  145. struct htc_packet *packet)
  146. {
  147. int status;
  148. bool sync = false;
  149. u32 padded_len, send_len;
  150. if (!packet->completion)
  151. sync = true;
  152. send_len = packet->act_len + HTC_HDR_LENGTH;
  153. padded_len = CALC_TXRX_PADDED_LEN(target, send_len);
  154. ath6kl_dbg(ATH6KL_DBG_HTC,
  155. "htc tx issue len %d padded_len %d mbox 0x%X %s\n",
  156. send_len, padded_len,
  157. target->dev->ar->mbox_info.htc_addr,
  158. sync ? "sync" : "async");
  159. if (sync) {
  160. status = hif_read_write_sync(target->dev->ar,
  161. target->dev->ar->mbox_info.htc_addr,
  162. packet->buf, padded_len,
  163. HIF_WR_SYNC_BLOCK_INC);
  164. packet->status = status;
  165. packet->buf += HTC_HDR_LENGTH;
  166. } else
  167. status = hif_write_async(target->dev->ar,
  168. target->dev->ar->mbox_info.htc_addr,
  169. packet->buf, padded_len,
  170. HIF_WR_ASYNC_BLOCK_INC, packet);
  171. return status;
  172. }
  173. static int htc_check_credits(struct htc_target *target,
  174. struct htc_endpoint *ep, u8 *flags,
  175. enum htc_endpoint_id eid, unsigned int len,
  176. int *req_cred)
  177. {
  178. *req_cred = (len > target->tgt_cred_sz) ?
  179. DIV_ROUND_UP(len, target->tgt_cred_sz) : 1;
  180. ath6kl_dbg(ATH6KL_DBG_HTC, "htc creds required %d got %d\n",
  181. *req_cred, ep->cred_dist.credits);
  182. if (ep->cred_dist.credits < *req_cred) {
  183. if (eid == ENDPOINT_0)
  184. return -EINVAL;
  185. /* Seek more credits */
  186. ep->cred_dist.seek_cred = *req_cred - ep->cred_dist.credits;
  187. ath6kl_dbg(ATH6KL_DBG_HTC, "htc creds ctxt 0x%p dist 0x%p\n",
  188. target->cred_dist_cntxt, &ep->cred_dist);
  189. ath6k_seek_credits(target->cred_dist_cntxt, &ep->cred_dist);
  190. ep->cred_dist.seek_cred = 0;
  191. if (ep->cred_dist.credits < *req_cred) {
  192. ath6kl_dbg(ATH6KL_DBG_HTC,
  193. "htc creds not enough credits for ep %d\n",
  194. eid);
  195. return -EINVAL;
  196. }
  197. }
  198. ep->cred_dist.credits -= *req_cred;
  199. ep->ep_st.cred_cosumd += *req_cred;
  200. /* When we are getting low on credits, ask for more */
  201. if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
  202. ep->cred_dist.seek_cred =
  203. ep->cred_dist.cred_per_msg - ep->cred_dist.credits;
  204. ath6kl_dbg(ATH6KL_DBG_HTC, "htc creds ctxt 0x%p dist 0x%p\n",
  205. target->cred_dist_cntxt, &ep->cred_dist);
  206. ath6k_seek_credits(target->cred_dist_cntxt, &ep->cred_dist);
  207. /* see if we were successful in getting more */
  208. if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
  209. /* tell the target we need credits ASAP! */
  210. *flags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  211. ep->ep_st.cred_low_indicate += 1;
  212. ath6kl_dbg(ATH6KL_DBG_HTC, "htc creds host needs credits\n");
  213. }
  214. }
  215. return 0;
  216. }
  217. static void ath6kl_htc_tx_pkts_get(struct htc_target *target,
  218. struct htc_endpoint *endpoint,
  219. struct list_head *queue)
  220. {
  221. int req_cred;
  222. u8 flags;
  223. struct htc_packet *packet;
  224. unsigned int len;
  225. while (true) {
  226. flags = 0;
  227. if (list_empty(&endpoint->txq))
  228. break;
  229. packet = list_first_entry(&endpoint->txq, struct htc_packet,
  230. list);
  231. ath6kl_dbg(ATH6KL_DBG_HTC,
  232. "htc tx got packet 0x%p queue depth %d\n",
  233. packet, get_queue_depth(&endpoint->txq));
  234. len = CALC_TXRX_PADDED_LEN(target,
  235. packet->act_len + HTC_HDR_LENGTH);
  236. if (htc_check_credits(target, endpoint, &flags,
  237. packet->endpoint, len, &req_cred))
  238. break;
  239. /* now we can fully move onto caller's queue */
  240. packet = list_first_entry(&endpoint->txq, struct htc_packet,
  241. list);
  242. list_move_tail(&packet->list, queue);
  243. /* save the number of credits this packet consumed */
  244. packet->info.tx.cred_used = req_cred;
  245. /* all TX packets are handled asynchronously */
  246. packet->completion = htc_tx_comp_handler;
  247. packet->context = target;
  248. endpoint->ep_st.tx_issued += 1;
  249. /* save send flags */
  250. packet->info.tx.flags = flags;
  251. packet->info.tx.seqno = endpoint->seqno;
  252. endpoint->seqno++;
  253. }
  254. }
  255. /* See if the padded tx length falls on a credit boundary */
  256. static int htc_get_credit_padding(unsigned int cred_sz, int *len,
  257. struct htc_endpoint *ep)
  258. {
  259. int rem_cred, cred_pad;
  260. rem_cred = *len % cred_sz;
  261. /* No padding needed */
  262. if (!rem_cred)
  263. return 0;
  264. if (!(ep->conn_flags & HTC_FLGS_TX_BNDL_PAD_EN))
  265. return -1;
  266. /*
  267. * The transfer consumes a "partial" credit, this
  268. * packet cannot be bundled unless we add
  269. * additional "dummy" padding (max 255 bytes) to
  270. * consume the entire credit.
  271. */
  272. cred_pad = *len < cred_sz ? (cred_sz - *len) : rem_cred;
  273. if ((cred_pad > 0) && (cred_pad <= 255))
  274. *len += cred_pad;
  275. else
  276. /* The amount of padding is too large, send as non-bundled */
  277. return -1;
  278. return cred_pad;
  279. }
  280. static int ath6kl_htc_tx_setup_scat_list(struct htc_target *target,
  281. struct htc_endpoint *endpoint,
  282. struct hif_scatter_req *scat_req,
  283. int n_scat,
  284. struct list_head *queue)
  285. {
  286. struct htc_packet *packet;
  287. int i, len, rem_scat, cred_pad;
  288. int status = 0;
  289. rem_scat = target->max_tx_bndl_sz;
  290. for (i = 0; i < n_scat; i++) {
  291. scat_req->scat_list[i].packet = NULL;
  292. if (list_empty(queue))
  293. break;
  294. packet = list_first_entry(queue, struct htc_packet, list);
  295. len = CALC_TXRX_PADDED_LEN(target,
  296. packet->act_len + HTC_HDR_LENGTH);
  297. cred_pad = htc_get_credit_padding(target->tgt_cred_sz,
  298. &len, endpoint);
  299. if (cred_pad < 0 || rem_scat < len) {
  300. status = -ENOSPC;
  301. break;
  302. }
  303. rem_scat -= len;
  304. /* now remove it from the queue */
  305. list_del(&packet->list);
  306. scat_req->scat_list[i].packet = packet;
  307. /* prepare packet and flag message as part of a send bundle */
  308. ath6kl_htc_tx_prep_pkt(packet,
  309. packet->info.tx.flags | HTC_FLAGS_SEND_BUNDLE,
  310. cred_pad, packet->info.tx.seqno);
  311. /* Make sure the buffer is 4-byte aligned */
  312. ath6kl_htc_tx_buf_align(&packet->buf,
  313. packet->act_len + HTC_HDR_LENGTH);
  314. scat_req->scat_list[i].buf = packet->buf;
  315. scat_req->scat_list[i].len = len;
  316. scat_req->len += len;
  317. scat_req->scat_entries++;
  318. ath6kl_dbg(ATH6KL_DBG_HTC,
  319. "htc tx adding (%d) pkt 0x%p len %d remaining %d\n",
  320. i, packet, len, rem_scat);
  321. }
  322. /* Roll back scatter setup in case of any failure */
  323. if (scat_req->scat_entries < HTC_MIN_HTC_MSGS_TO_BUNDLE) {
  324. for (i = scat_req->scat_entries - 1; i >= 0; i--) {
  325. packet = scat_req->scat_list[i].packet;
  326. if (packet) {
  327. packet->buf += HTC_HDR_LENGTH;
  328. list_add(&packet->list, queue);
  329. }
  330. }
  331. return -EAGAIN;
  332. }
  333. return status;
  334. }
  335. /*
  336. * Drain a queue and send as bundles this function may return without fully
  337. * draining the queue when
  338. *
  339. * 1. scatter resources are exhausted
  340. * 2. a message that will consume a partial credit will stop the
  341. * bundling process early
  342. * 3. we drop below the minimum number of messages for a bundle
  343. */
  344. static void ath6kl_htc_tx_bundle(struct htc_endpoint *endpoint,
  345. struct list_head *queue,
  346. int *sent_bundle, int *n_bundle_pkts)
  347. {
  348. struct htc_target *target = endpoint->target;
  349. struct hif_scatter_req *scat_req = NULL;
  350. int n_scat, n_sent_bundle = 0, tot_pkts_bundle = 0;
  351. int status;
  352. while (true) {
  353. status = 0;
  354. n_scat = get_queue_depth(queue);
  355. n_scat = min(n_scat, target->msg_per_bndl_max);
  356. if (n_scat < HTC_MIN_HTC_MSGS_TO_BUNDLE)
  357. /* not enough to bundle */
  358. break;
  359. scat_req = hif_scatter_req_get(target->dev->ar);
  360. if (!scat_req) {
  361. /* no scatter resources */
  362. ath6kl_dbg(ATH6KL_DBG_HTC,
  363. "htc tx no more scatter resources\n");
  364. break;
  365. }
  366. ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx pkts to scatter: %d\n",
  367. n_scat);
  368. scat_req->len = 0;
  369. scat_req->scat_entries = 0;
  370. status = ath6kl_htc_tx_setup_scat_list(target, endpoint,
  371. scat_req, n_scat,
  372. queue);
  373. if (status == -EAGAIN) {
  374. hif_scatter_req_add(target->dev->ar, scat_req);
  375. break;
  376. }
  377. /* send path is always asynchronous */
  378. scat_req->complete = htc_async_tx_scat_complete;
  379. n_sent_bundle++;
  380. tot_pkts_bundle += scat_req->scat_entries;
  381. ath6kl_dbg(ATH6KL_DBG_HTC,
  382. "htc tx scatter bytes %d entries %d\n",
  383. scat_req->len, scat_req->scat_entries);
  384. ath6kl_hif_submit_scat_req(target->dev, scat_req, false);
  385. if (status)
  386. break;
  387. }
  388. *sent_bundle = n_sent_bundle;
  389. *n_bundle_pkts = tot_pkts_bundle;
  390. ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx bundle sent %d pkts\n",
  391. n_sent_bundle);
  392. return;
  393. }
  394. static void ath6kl_htc_tx_from_queue(struct htc_target *target,
  395. struct htc_endpoint *endpoint)
  396. {
  397. struct list_head txq;
  398. struct htc_packet *packet;
  399. int bundle_sent;
  400. int n_pkts_bundle;
  401. spin_lock_bh(&target->tx_lock);
  402. endpoint->tx_proc_cnt++;
  403. if (endpoint->tx_proc_cnt > 1) {
  404. endpoint->tx_proc_cnt--;
  405. spin_unlock_bh(&target->tx_lock);
  406. ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx busy\n");
  407. return;
  408. }
  409. /*
  410. * drain the endpoint TX queue for transmission as long
  411. * as we have enough credits.
  412. */
  413. INIT_LIST_HEAD(&txq);
  414. while (true) {
  415. if (list_empty(&endpoint->txq))
  416. break;
  417. ath6kl_htc_tx_pkts_get(target, endpoint, &txq);
  418. if (list_empty(&txq))
  419. break;
  420. spin_unlock_bh(&target->tx_lock);
  421. bundle_sent = 0;
  422. n_pkts_bundle = 0;
  423. while (true) {
  424. /* try to send a bundle on each pass */
  425. if ((target->tx_bndl_enable) &&
  426. (get_queue_depth(&txq) >=
  427. HTC_MIN_HTC_MSGS_TO_BUNDLE)) {
  428. int temp1 = 0, temp2 = 0;
  429. ath6kl_htc_tx_bundle(endpoint, &txq,
  430. &temp1, &temp2);
  431. bundle_sent += temp1;
  432. n_pkts_bundle += temp2;
  433. }
  434. if (list_empty(&txq))
  435. break;
  436. packet = list_first_entry(&txq, struct htc_packet,
  437. list);
  438. list_del(&packet->list);
  439. ath6kl_htc_tx_prep_pkt(packet, packet->info.tx.flags,
  440. 0, packet->info.tx.seqno);
  441. ath6kl_htc_tx_issue(target, packet);
  442. }
  443. spin_lock_bh(&target->tx_lock);
  444. endpoint->ep_st.tx_bundles += bundle_sent;
  445. endpoint->ep_st.tx_pkt_bundled += n_pkts_bundle;
  446. }
  447. endpoint->tx_proc_cnt = 0;
  448. spin_unlock_bh(&target->tx_lock);
  449. }
  450. static bool ath6kl_htc_tx_try(struct htc_target *target,
  451. struct htc_endpoint *endpoint,
  452. struct htc_packet *tx_pkt)
  453. {
  454. struct htc_ep_callbacks ep_cb;
  455. int txq_depth;
  456. bool overflow = false;
  457. ep_cb = endpoint->ep_cb;
  458. spin_lock_bh(&target->tx_lock);
  459. txq_depth = get_queue_depth(&endpoint->txq);
  460. spin_unlock_bh(&target->tx_lock);
  461. if (txq_depth >= endpoint->max_txq_depth)
  462. overflow = true;
  463. if (overflow)
  464. ath6kl_dbg(ATH6KL_DBG_HTC,
  465. "htc tx overflow ep %d depth %d max %d\n",
  466. endpoint->eid, txq_depth,
  467. endpoint->max_txq_depth);
  468. if (overflow && ep_cb.tx_full) {
  469. if (ep_cb.tx_full(endpoint->target, tx_pkt) ==
  470. HTC_SEND_FULL_DROP) {
  471. endpoint->ep_st.tx_dropped += 1;
  472. return false;
  473. }
  474. }
  475. spin_lock_bh(&target->tx_lock);
  476. list_add_tail(&tx_pkt->list, &endpoint->txq);
  477. spin_unlock_bh(&target->tx_lock);
  478. ath6kl_htc_tx_from_queue(target, endpoint);
  479. return true;
  480. }
  481. static void htc_chk_ep_txq(struct htc_target *target)
  482. {
  483. struct htc_endpoint *endpoint;
  484. struct htc_endpoint_credit_dist *cred_dist;
  485. /*
  486. * Run through the credit distribution list to see if there are
  487. * packets queued. NOTE: no locks need to be taken since the
  488. * distribution list is not dynamic (cannot be re-ordered) and we
  489. * are not modifying any state.
  490. */
  491. list_for_each_entry(cred_dist, &target->cred_dist_list, list) {
  492. endpoint = (struct htc_endpoint *)cred_dist->htc_rsvd;
  493. spin_lock_bh(&target->tx_lock);
  494. if (!list_empty(&endpoint->txq)) {
  495. ath6kl_dbg(ATH6KL_DBG_HTC,
  496. "htc creds ep %d credits %d pkts %d\n",
  497. cred_dist->endpoint,
  498. endpoint->cred_dist.credits,
  499. get_queue_depth(&endpoint->txq));
  500. spin_unlock_bh(&target->tx_lock);
  501. /*
  502. * Try to start the stalled queue, this list is
  503. * ordered by priority. If there are credits
  504. * available the highest priority queue will get a
  505. * chance to reclaim credits from lower priority
  506. * ones.
  507. */
  508. ath6kl_htc_tx_from_queue(target, endpoint);
  509. spin_lock_bh(&target->tx_lock);
  510. }
  511. spin_unlock_bh(&target->tx_lock);
  512. }
  513. }
  514. static int htc_setup_tx_complete(struct htc_target *target)
  515. {
  516. struct htc_packet *send_pkt = NULL;
  517. int status;
  518. send_pkt = htc_get_control_buf(target, true);
  519. if (!send_pkt)
  520. return -ENOMEM;
  521. if (target->htc_tgt_ver >= HTC_VERSION_2P1) {
  522. struct htc_setup_comp_ext_msg *setup_comp_ext;
  523. u32 flags = 0;
  524. setup_comp_ext =
  525. (struct htc_setup_comp_ext_msg *)send_pkt->buf;
  526. memset(setup_comp_ext, 0, sizeof(*setup_comp_ext));
  527. setup_comp_ext->msg_id =
  528. cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);
  529. if (target->msg_per_bndl_max > 0) {
  530. /* Indicate HTC bundling to the target */
  531. flags |= HTC_SETUP_COMP_FLG_RX_BNDL_EN;
  532. setup_comp_ext->msg_per_rxbndl =
  533. target->msg_per_bndl_max;
  534. }
  535. memcpy(&setup_comp_ext->flags, &flags,
  536. sizeof(setup_comp_ext->flags));
  537. set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp_ext,
  538. sizeof(struct htc_setup_comp_ext_msg),
  539. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  540. } else {
  541. struct htc_setup_comp_msg *setup_comp;
  542. setup_comp = (struct htc_setup_comp_msg *)send_pkt->buf;
  543. memset(setup_comp, 0, sizeof(struct htc_setup_comp_msg));
  544. setup_comp->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_ID);
  545. set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp,
  546. sizeof(struct htc_setup_comp_msg),
  547. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  548. }
  549. /* we want synchronous operation */
  550. send_pkt->completion = NULL;
  551. ath6kl_htc_tx_prep_pkt(send_pkt, 0, 0, 0);
  552. status = ath6kl_htc_tx_issue(target, send_pkt);
  553. if (send_pkt != NULL)
  554. htc_reclaim_txctrl_buf(target, send_pkt);
  555. return status;
  556. }
  557. void ath6kl_htc_set_credit_dist(struct htc_target *target,
  558. struct htc_credit_state_info *cred_dist_cntxt,
  559. u16 srvc_pri_order[], int list_len)
  560. {
  561. struct htc_endpoint *endpoint;
  562. int i, ep;
  563. target->cred_dist_cntxt = cred_dist_cntxt;
  564. list_add_tail(&target->endpoint[ENDPOINT_0].cred_dist.list,
  565. &target->cred_dist_list);
  566. for (i = 0; i < list_len; i++) {
  567. for (ep = ENDPOINT_1; ep < ENDPOINT_MAX; ep++) {
  568. endpoint = &target->endpoint[ep];
  569. if (endpoint->svc_id == srvc_pri_order[i]) {
  570. list_add_tail(&endpoint->cred_dist.list,
  571. &target->cred_dist_list);
  572. break;
  573. }
  574. }
  575. if (ep >= ENDPOINT_MAX) {
  576. WARN_ON(1);
  577. return;
  578. }
  579. }
  580. }
  581. int ath6kl_htc_tx(struct htc_target *target, struct htc_packet *packet)
  582. {
  583. struct htc_endpoint *endpoint;
  584. struct list_head queue;
  585. ath6kl_dbg(ATH6KL_DBG_HTC,
  586. "htc tx ep id %d buf 0x%p len %d\n",
  587. packet->endpoint, packet->buf, packet->act_len);
  588. if (packet->endpoint >= ENDPOINT_MAX) {
  589. WARN_ON(1);
  590. return -EINVAL;
  591. }
  592. endpoint = &target->endpoint[packet->endpoint];
  593. if (!ath6kl_htc_tx_try(target, endpoint, packet)) {
  594. packet->status = (target->htc_flags & HTC_OP_STATE_STOPPING) ?
  595. -ECANCELED : -ENOSPC;
  596. INIT_LIST_HEAD(&queue);
  597. list_add(&packet->list, &queue);
  598. htc_tx_complete(endpoint, &queue);
  599. }
  600. return 0;
  601. }
  602. /* flush endpoint TX queue */
  603. void ath6kl_htc_flush_txep(struct htc_target *target,
  604. enum htc_endpoint_id eid, u16 tag)
  605. {
  606. struct htc_packet *packet, *tmp_pkt;
  607. struct list_head discard_q, container;
  608. struct htc_endpoint *endpoint = &target->endpoint[eid];
  609. if (!endpoint->svc_id) {
  610. WARN_ON(1);
  611. return;
  612. }
  613. /* initialize the discard queue */
  614. INIT_LIST_HEAD(&discard_q);
  615. spin_lock_bh(&target->tx_lock);
  616. list_for_each_entry_safe(packet, tmp_pkt, &endpoint->txq, list) {
  617. if ((tag == HTC_TX_PACKET_TAG_ALL) ||
  618. (tag == packet->info.tx.tag))
  619. list_move_tail(&packet->list, &discard_q);
  620. }
  621. spin_unlock_bh(&target->tx_lock);
  622. list_for_each_entry_safe(packet, tmp_pkt, &discard_q, list) {
  623. packet->status = -ECANCELED;
  624. list_del(&packet->list);
  625. ath6kl_dbg(ATH6KL_DBG_HTC,
  626. "htc tx flushing pkt 0x%p len %d ep %d tag 0x%x\n",
  627. packet, packet->act_len,
  628. packet->endpoint, packet->info.tx.tag);
  629. INIT_LIST_HEAD(&container);
  630. list_add_tail(&packet->list, &container);
  631. htc_tx_complete(endpoint, &container);
  632. }
  633. }
  634. static void ath6kl_htc_flush_txep_all(struct htc_target *target)
  635. {
  636. struct htc_endpoint *endpoint;
  637. int i;
  638. dump_cred_dist_stats(target);
  639. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  640. endpoint = &target->endpoint[i];
  641. if (endpoint->svc_id == 0)
  642. /* not in use.. */
  643. continue;
  644. ath6kl_htc_flush_txep(target, i, HTC_TX_PACKET_TAG_ALL);
  645. }
  646. }
  647. void ath6kl_htc_indicate_activity_change(struct htc_target *target,
  648. enum htc_endpoint_id eid, bool active)
  649. {
  650. struct htc_endpoint *endpoint = &target->endpoint[eid];
  651. bool dist = false;
  652. if (endpoint->svc_id == 0) {
  653. WARN_ON(1);
  654. return;
  655. }
  656. spin_lock_bh(&target->tx_lock);
  657. if (active) {
  658. if (!(endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE)) {
  659. endpoint->cred_dist.dist_flags |= HTC_EP_ACTIVE;
  660. dist = true;
  661. }
  662. } else {
  663. if (endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE) {
  664. endpoint->cred_dist.dist_flags &= ~HTC_EP_ACTIVE;
  665. dist = true;
  666. }
  667. }
  668. if (dist) {
  669. endpoint->cred_dist.txq_depth =
  670. get_queue_depth(&endpoint->txq);
  671. ath6kl_dbg(ATH6KL_DBG_HTC,
  672. "htc tx activity ctxt 0x%p dist 0x%p\n",
  673. target->cred_dist_cntxt, &target->cred_dist_list);
  674. ath6k_credit_distribute(target->cred_dist_cntxt,
  675. &target->cred_dist_list,
  676. HTC_CREDIT_DIST_ACTIVITY_CHANGE);
  677. }
  678. spin_unlock_bh(&target->tx_lock);
  679. if (dist && !active)
  680. htc_chk_ep_txq(target);
  681. }
  682. /* HTC Rx */
  683. static inline void ath6kl_htc_rx_update_stats(struct htc_endpoint *endpoint,
  684. int n_look_ahds)
  685. {
  686. endpoint->ep_st.rx_pkts++;
  687. if (n_look_ahds == 1)
  688. endpoint->ep_st.rx_lkahds++;
  689. else if (n_look_ahds > 1)
  690. endpoint->ep_st.rx_bundle_lkahd++;
  691. }
  692. static inline bool htc_valid_rx_frame_len(struct htc_target *target,
  693. enum htc_endpoint_id eid, int len)
  694. {
  695. return (eid == target->dev->ar->ctrl_ep) ?
  696. len <= ATH6KL_BUFFER_SIZE : len <= ATH6KL_AMSDU_BUFFER_SIZE;
  697. }
  698. static int htc_add_rxbuf(struct htc_target *target, struct htc_packet *packet)
  699. {
  700. struct list_head queue;
  701. INIT_LIST_HEAD(&queue);
  702. list_add_tail(&packet->list, &queue);
  703. return ath6kl_htc_add_rxbuf_multiple(target, &queue);
  704. }
  705. static void htc_reclaim_rxbuf(struct htc_target *target,
  706. struct htc_packet *packet,
  707. struct htc_endpoint *ep)
  708. {
  709. if (packet->info.rx.rx_flags & HTC_RX_PKT_NO_RECYCLE) {
  710. htc_rxpkt_reset(packet);
  711. packet->status = -ECANCELED;
  712. ep->ep_cb.rx(ep->target, packet);
  713. } else {
  714. htc_rxpkt_reset(packet);
  715. htc_add_rxbuf((void *)(target), packet);
  716. }
  717. }
  718. static void reclaim_rx_ctrl_buf(struct htc_target *target,
  719. struct htc_packet *packet)
  720. {
  721. spin_lock_bh(&target->htc_lock);
  722. list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
  723. spin_unlock_bh(&target->htc_lock);
  724. }
  725. static int ath6kl_htc_rx_packet(struct htc_target *target,
  726. struct htc_packet *packet,
  727. u32 rx_len)
  728. {
  729. struct ath6kl_device *dev = target->dev;
  730. u32 padded_len;
  731. int status;
  732. padded_len = CALC_TXRX_PADDED_LEN(target, rx_len);
  733. if (padded_len > packet->buf_len) {
  734. ath6kl_err("not enough receive space for packet - padlen %d recvlen %d bufferlen %d\n",
  735. padded_len, rx_len, packet->buf_len);
  736. return -ENOMEM;
  737. }
  738. ath6kl_dbg(ATH6KL_DBG_HTC,
  739. "htc rx 0x%p hdr x%x len %d mbox 0x%x\n",
  740. packet, packet->info.rx.exp_hdr,
  741. padded_len, dev->ar->mbox_info.htc_addr);
  742. status = hif_read_write_sync(dev->ar,
  743. dev->ar->mbox_info.htc_addr,
  744. packet->buf, padded_len,
  745. HIF_RD_SYNC_BLOCK_FIX);
  746. packet->status = status;
  747. return status;
  748. }
  749. /*
  750. * optimization for recv packets, we can indicate a
  751. * "hint" that there are more single-packets to fetch
  752. * on this endpoint.
  753. */
  754. static void ath6kl_htc_rx_set_indicate(u32 lk_ahd,
  755. struct htc_endpoint *endpoint,
  756. struct htc_packet *packet)
  757. {
  758. struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)&lk_ahd;
  759. if (htc_hdr->eid == packet->endpoint) {
  760. if (!list_empty(&endpoint->rx_bufq))
  761. packet->info.rx.indicat_flags |=
  762. HTC_RX_FLAGS_INDICATE_MORE_PKTS;
  763. }
  764. }
  765. static void ath6kl_htc_rx_chk_water_mark(struct htc_endpoint *endpoint)
  766. {
  767. struct htc_ep_callbacks ep_cb = endpoint->ep_cb;
  768. if (ep_cb.rx_refill_thresh > 0) {
  769. spin_lock_bh(&endpoint->target->rx_lock);
  770. if (get_queue_depth(&endpoint->rx_bufq)
  771. < ep_cb.rx_refill_thresh) {
  772. spin_unlock_bh(&endpoint->target->rx_lock);
  773. ep_cb.rx_refill(endpoint->target, endpoint->eid);
  774. return;
  775. }
  776. spin_unlock_bh(&endpoint->target->rx_lock);
  777. }
  778. }
  779. /* This function is called with rx_lock held */
  780. static int ath6kl_htc_rx_setup(struct htc_target *target,
  781. struct htc_endpoint *ep,
  782. u32 *lk_ahds, struct list_head *queue, int n_msg)
  783. {
  784. struct htc_packet *packet;
  785. /* FIXME: type of lk_ahds can't be right */
  786. struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)lk_ahds;
  787. struct htc_ep_callbacks ep_cb;
  788. int status = 0, j, full_len;
  789. bool no_recycle;
  790. full_len = CALC_TXRX_PADDED_LEN(target,
  791. le16_to_cpu(htc_hdr->payld_len) +
  792. sizeof(*htc_hdr));
  793. if (!htc_valid_rx_frame_len(target, ep->eid, full_len)) {
  794. ath6kl_warn("Rx buffer requested with invalid length\n");
  795. return -EINVAL;
  796. }
  797. ep_cb = ep->ep_cb;
  798. for (j = 0; j < n_msg; j++) {
  799. /*
  800. * Reset flag, any packets allocated using the
  801. * rx_alloc() API cannot be recycled on
  802. * cleanup,they must be explicitly returned.
  803. */
  804. no_recycle = false;
  805. if (ep_cb.rx_allocthresh &&
  806. (full_len > ep_cb.rx_alloc_thresh)) {
  807. ep->ep_st.rx_alloc_thresh_hit += 1;
  808. ep->ep_st.rxalloc_thresh_byte +=
  809. le16_to_cpu(htc_hdr->payld_len);
  810. spin_unlock_bh(&target->rx_lock);
  811. no_recycle = true;
  812. packet = ep_cb.rx_allocthresh(ep->target, ep->eid,
  813. full_len);
  814. spin_lock_bh(&target->rx_lock);
  815. } else {
  816. /* refill handler is being used */
  817. if (list_empty(&ep->rx_bufq)) {
  818. if (ep_cb.rx_refill) {
  819. spin_unlock_bh(&target->rx_lock);
  820. ep_cb.rx_refill(ep->target, ep->eid);
  821. spin_lock_bh(&target->rx_lock);
  822. }
  823. }
  824. if (list_empty(&ep->rx_bufq))
  825. packet = NULL;
  826. else {
  827. packet = list_first_entry(&ep->rx_bufq,
  828. struct htc_packet, list);
  829. list_del(&packet->list);
  830. }
  831. }
  832. if (!packet) {
  833. target->rx_st_flags |= HTC_RECV_WAIT_BUFFERS;
  834. target->ep_waiting = ep->eid;
  835. return -ENOSPC;
  836. }
  837. /* clear flags */
  838. packet->info.rx.rx_flags = 0;
  839. packet->info.rx.indicat_flags = 0;
  840. packet->status = 0;
  841. if (no_recycle)
  842. /*
  843. * flag that these packets cannot be
  844. * recycled, they have to be returned to
  845. * the user
  846. */
  847. packet->info.rx.rx_flags |= HTC_RX_PKT_NO_RECYCLE;
  848. /* Caller needs to free this upon any failure */
  849. list_add_tail(&packet->list, queue);
  850. if (target->htc_flags & HTC_OP_STATE_STOPPING) {
  851. status = -ECANCELED;
  852. break;
  853. }
  854. if (j) {
  855. packet->info.rx.rx_flags |= HTC_RX_PKT_REFRESH_HDR;
  856. packet->info.rx.exp_hdr = 0xFFFFFFFF;
  857. } else
  858. /* set expected look ahead */
  859. packet->info.rx.exp_hdr = *lk_ahds;
  860. packet->act_len = le16_to_cpu(htc_hdr->payld_len) +
  861. HTC_HDR_LENGTH;
  862. }
  863. return status;
  864. }
  865. static int ath6kl_htc_rx_alloc(struct htc_target *target,
  866. u32 lk_ahds[], int msg,
  867. struct htc_endpoint *endpoint,
  868. struct list_head *queue)
  869. {
  870. int status = 0;
  871. struct htc_packet *packet, *tmp_pkt;
  872. struct htc_frame_hdr *htc_hdr;
  873. int i, n_msg;
  874. spin_lock_bh(&target->rx_lock);
  875. for (i = 0; i < msg; i++) {
  876. htc_hdr = (struct htc_frame_hdr *)&lk_ahds[i];
  877. if (htc_hdr->eid >= ENDPOINT_MAX) {
  878. ath6kl_err("invalid ep in look-ahead: %d\n",
  879. htc_hdr->eid);
  880. status = -ENOMEM;
  881. break;
  882. }
  883. if (htc_hdr->eid != endpoint->eid) {
  884. ath6kl_err("invalid ep in look-ahead: %d should be : %d (index:%d)\n",
  885. htc_hdr->eid, endpoint->eid, i);
  886. status = -ENOMEM;
  887. break;
  888. }
  889. if (le16_to_cpu(htc_hdr->payld_len) > HTC_MAX_PAYLOAD_LENGTH) {
  890. ath6kl_err("payload len %d exceeds max htc : %d !\n",
  891. htc_hdr->payld_len,
  892. (u32) HTC_MAX_PAYLOAD_LENGTH);
  893. status = -ENOMEM;
  894. break;
  895. }
  896. if (endpoint->svc_id == 0) {
  897. ath6kl_err("ep %d is not connected !\n", htc_hdr->eid);
  898. status = -ENOMEM;
  899. break;
  900. }
  901. if (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) {
  902. /*
  903. * HTC header indicates that every packet to follow
  904. * has the same padded length so that it can be
  905. * optimally fetched as a full bundle.
  906. */
  907. n_msg = (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) >>
  908. HTC_FLG_RX_BNDL_CNT_S;
  909. /* the count doesn't include the starter frame */
  910. n_msg++;
  911. if (n_msg > target->msg_per_bndl_max) {
  912. status = -ENOMEM;
  913. break;
  914. }
  915. endpoint->ep_st.rx_bundle_from_hdr += 1;
  916. ath6kl_dbg(ATH6KL_DBG_HTC,
  917. "htc rx bundle pkts %d\n",
  918. n_msg);
  919. } else
  920. /* HTC header only indicates 1 message to fetch */
  921. n_msg = 1;
  922. /* Setup packet buffers for each message */
  923. status = ath6kl_htc_rx_setup(target, endpoint, &lk_ahds[i],
  924. queue, n_msg);
  925. /*
  926. * This is due to unavailabilty of buffers to rx entire data.
  927. * Return no error so that free buffers from queue can be used
  928. * to receive partial data.
  929. */
  930. if (status == -ENOSPC) {
  931. spin_unlock_bh(&target->rx_lock);
  932. return 0;
  933. }
  934. if (status)
  935. break;
  936. }
  937. spin_unlock_bh(&target->rx_lock);
  938. if (status) {
  939. list_for_each_entry_safe(packet, tmp_pkt, queue, list) {
  940. list_del(&packet->list);
  941. htc_reclaim_rxbuf(target, packet,
  942. &target->endpoint[packet->endpoint]);
  943. }
  944. }
  945. return status;
  946. }
  947. static void htc_ctrl_rx(struct htc_target *context, struct htc_packet *packets)
  948. {
  949. if (packets->endpoint != ENDPOINT_0) {
  950. WARN_ON(1);
  951. return;
  952. }
  953. if (packets->status == -ECANCELED) {
  954. reclaim_rx_ctrl_buf(context, packets);
  955. return;
  956. }
  957. if (packets->act_len > 0) {
  958. ath6kl_err("htc_ctrl_rx, got message with len:%zu\n",
  959. packets->act_len + HTC_HDR_LENGTH);
  960. ath6kl_dbg_dump(ATH6KL_DBG_HTC,
  961. "htc rx unexpected endpoint 0 message", "",
  962. packets->buf - HTC_HDR_LENGTH,
  963. packets->act_len + HTC_HDR_LENGTH);
  964. }
  965. htc_reclaim_rxbuf(context, packets, &context->endpoint[0]);
  966. }
  967. static void htc_proc_cred_rpt(struct htc_target *target,
  968. struct htc_credit_report *rpt,
  969. int n_entries,
  970. enum htc_endpoint_id from_ep)
  971. {
  972. struct htc_endpoint *endpoint;
  973. int tot_credits = 0, i;
  974. bool dist = false;
  975. ath6kl_dbg(ATH6KL_DBG_HTC,
  976. "htc creds report entries %d\n", n_entries);
  977. spin_lock_bh(&target->tx_lock);
  978. for (i = 0; i < n_entries; i++, rpt++) {
  979. if (rpt->eid >= ENDPOINT_MAX) {
  980. WARN_ON(1);
  981. spin_unlock_bh(&target->tx_lock);
  982. return;
  983. }
  984. endpoint = &target->endpoint[rpt->eid];
  985. ath6kl_dbg(ATH6KL_DBG_HTC,
  986. "htc creds report ep %d credits %d\n",
  987. rpt->eid, rpt->credits);
  988. endpoint->ep_st.tx_cred_rpt += 1;
  989. endpoint->ep_st.cred_retnd += rpt->credits;
  990. if (from_ep == rpt->eid) {
  991. /*
  992. * This credit report arrived on the same endpoint
  993. * indicating it arrived in an RX packet.
  994. */
  995. endpoint->ep_st.cred_from_rx += rpt->credits;
  996. endpoint->ep_st.cred_rpt_from_rx += 1;
  997. } else if (from_ep == ENDPOINT_0) {
  998. /* credit arrived on endpoint 0 as a NULL message */
  999. endpoint->ep_st.cred_from_ep0 += rpt->credits;
  1000. endpoint->ep_st.cred_rpt_ep0 += 1;
  1001. } else {
  1002. endpoint->ep_st.cred_from_other += rpt->credits;
  1003. endpoint->ep_st.cred_rpt_from_other += 1;
  1004. }
  1005. if (rpt->eid == ENDPOINT_0)
  1006. /* always give endpoint 0 credits back */
  1007. endpoint->cred_dist.credits += rpt->credits;
  1008. else {
  1009. endpoint->cred_dist.cred_to_dist += rpt->credits;
  1010. dist = true;
  1011. }
  1012. /*
  1013. * Refresh tx depth for distribution function that will
  1014. * recover these credits NOTE: this is only valid when
  1015. * there are credits to recover!
  1016. */
  1017. endpoint->cred_dist.txq_depth =
  1018. get_queue_depth(&endpoint->txq);
  1019. tot_credits += rpt->credits;
  1020. }
  1021. ath6kl_dbg(ATH6KL_DBG_HTC,
  1022. "htc creds report tot_credits %d\n",
  1023. tot_credits);
  1024. if (dist) {
  1025. /*
  1026. * This was a credit return based on a completed send
  1027. * operations note, this is done with the lock held
  1028. */
  1029. ath6kl_dbg(ATH6KL_DBG_HTC, "htc creds ctxt 0x%p dist 0x%p\n",
  1030. target->cred_dist_cntxt, &target->cred_dist_list);
  1031. ath6k_credit_distribute(target->cred_dist_cntxt,
  1032. &target->cred_dist_list,
  1033. HTC_CREDIT_DIST_SEND_COMPLETE);
  1034. }
  1035. spin_unlock_bh(&target->tx_lock);
  1036. if (tot_credits)
  1037. htc_chk_ep_txq(target);
  1038. }
  1039. static int htc_parse_trailer(struct htc_target *target,
  1040. struct htc_record_hdr *record,
  1041. u8 *record_buf, u32 *next_lk_ahds,
  1042. enum htc_endpoint_id endpoint,
  1043. int *n_lk_ahds)
  1044. {
  1045. struct htc_bundle_lkahd_rpt *bundle_lkahd_rpt;
  1046. struct htc_lookahead_report *lk_ahd;
  1047. int len;
  1048. switch (record->rec_id) {
  1049. case HTC_RECORD_CREDITS:
  1050. len = record->len / sizeof(struct htc_credit_report);
  1051. if (!len) {
  1052. WARN_ON(1);
  1053. return -EINVAL;
  1054. }
  1055. htc_proc_cred_rpt(target,
  1056. (struct htc_credit_report *) record_buf,
  1057. len, endpoint);
  1058. break;
  1059. case HTC_RECORD_LOOKAHEAD:
  1060. len = record->len / sizeof(*lk_ahd);
  1061. if (!len) {
  1062. WARN_ON(1);
  1063. return -EINVAL;
  1064. }
  1065. lk_ahd = (struct htc_lookahead_report *) record_buf;
  1066. if ((lk_ahd->pre_valid == ((~lk_ahd->post_valid) & 0xFF))
  1067. && next_lk_ahds) {
  1068. ath6kl_dbg(ATH6KL_DBG_HTC,
  1069. "htc rx lk_ahd found pre_valid 0x%x post_valid 0x%x\n",
  1070. lk_ahd->pre_valid, lk_ahd->post_valid);
  1071. /* look ahead bytes are valid, copy them over */
  1072. memcpy((u8 *)&next_lk_ahds[0], lk_ahd->lk_ahd, 4);
  1073. ath6kl_dbg_dump(ATH6KL_DBG_HTC,
  1074. "htc rx next look ahead",
  1075. "", next_lk_ahds, 4);
  1076. *n_lk_ahds = 1;
  1077. }
  1078. break;
  1079. case HTC_RECORD_LOOKAHEAD_BUNDLE:
  1080. len = record->len / sizeof(*bundle_lkahd_rpt);
  1081. if (!len || (len > HTC_HOST_MAX_MSG_PER_BUNDLE)) {
  1082. WARN_ON(1);
  1083. return -EINVAL;
  1084. }
  1085. if (next_lk_ahds) {
  1086. int i;
  1087. bundle_lkahd_rpt =
  1088. (struct htc_bundle_lkahd_rpt *) record_buf;
  1089. ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bundle lk_ahd",
  1090. "", record_buf, record->len);
  1091. for (i = 0; i < len; i++) {
  1092. memcpy((u8 *)&next_lk_ahds[i],
  1093. bundle_lkahd_rpt->lk_ahd, 4);
  1094. bundle_lkahd_rpt++;
  1095. }
  1096. *n_lk_ahds = i;
  1097. }
  1098. break;
  1099. default:
  1100. ath6kl_err("unhandled record: id:%d len:%d\n",
  1101. record->rec_id, record->len);
  1102. break;
  1103. }
  1104. return 0;
  1105. }
  1106. static int htc_proc_trailer(struct htc_target *target,
  1107. u8 *buf, int len, u32 *next_lk_ahds,
  1108. int *n_lk_ahds, enum htc_endpoint_id endpoint)
  1109. {
  1110. struct htc_record_hdr *record;
  1111. int orig_len;
  1112. int status;
  1113. u8 *record_buf;
  1114. u8 *orig_buf;
  1115. ath6kl_dbg(ATH6KL_DBG_HTC, "htc rx trailer len %d\n", len);
  1116. ath6kl_dbg_dump(ATH6KL_DBG_HTC, NULL, "", buf, len);
  1117. orig_buf = buf;
  1118. orig_len = len;
  1119. status = 0;
  1120. while (len > 0) {
  1121. if (len < sizeof(struct htc_record_hdr)) {
  1122. status = -ENOMEM;
  1123. break;
  1124. }
  1125. /* these are byte aligned structs */
  1126. record = (struct htc_record_hdr *) buf;
  1127. len -= sizeof(struct htc_record_hdr);
  1128. buf += sizeof(struct htc_record_hdr);
  1129. if (record->len > len) {
  1130. ath6kl_err("invalid record len: %d (id:%d) buf has: %d bytes left\n",
  1131. record->len, record->rec_id, len);
  1132. status = -ENOMEM;
  1133. break;
  1134. }
  1135. record_buf = buf;
  1136. status = htc_parse_trailer(target, record, record_buf,
  1137. next_lk_ahds, endpoint, n_lk_ahds);
  1138. if (status)
  1139. break;
  1140. /* advance buffer past this record for next time around */
  1141. buf += record->len;
  1142. len -= record->len;
  1143. }
  1144. if (status)
  1145. ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad trailer",
  1146. "", orig_buf, orig_len);
  1147. return status;
  1148. }
  1149. static int ath6kl_htc_rx_process_hdr(struct htc_target *target,
  1150. struct htc_packet *packet,
  1151. u32 *next_lkahds, int *n_lkahds)
  1152. {
  1153. int status = 0;
  1154. u16 payload_len;
  1155. u32 lk_ahd;
  1156. struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)packet->buf;
  1157. if (n_lkahds != NULL)
  1158. *n_lkahds = 0;
  1159. /*
  1160. * NOTE: we cannot assume the alignment of buf, so we use the safe
  1161. * macros to retrieve 16 bit fields.
  1162. */
  1163. payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
  1164. memcpy((u8 *)&lk_ahd, packet->buf, sizeof(lk_ahd));
  1165. if (packet->info.rx.rx_flags & HTC_RX_PKT_REFRESH_HDR) {
  1166. /*
  1167. * Refresh the expected header and the actual length as it
  1168. * was unknown when this packet was grabbed as part of the
  1169. * bundle.
  1170. */
  1171. packet->info.rx.exp_hdr = lk_ahd;
  1172. packet->act_len = payload_len + HTC_HDR_LENGTH;
  1173. /* validate the actual header that was refreshed */
  1174. if (packet->act_len > packet->buf_len) {
  1175. ath6kl_err("refreshed hdr payload len (%d) in bundled recv is invalid (hdr: 0x%X)\n",
  1176. payload_len, lk_ahd);
  1177. /*
  1178. * Limit this to max buffer just to print out some
  1179. * of the buffer.
  1180. */
  1181. packet->act_len = min(packet->act_len, packet->buf_len);
  1182. status = -ENOMEM;
  1183. goto fail_rx;
  1184. }
  1185. if (packet->endpoint != htc_hdr->eid) {
  1186. ath6kl_err("refreshed hdr ep (%d) does not match expected ep (%d)\n",
  1187. htc_hdr->eid, packet->endpoint);
  1188. status = -ENOMEM;
  1189. goto fail_rx;
  1190. }
  1191. }
  1192. if (lk_ahd != packet->info.rx.exp_hdr) {
  1193. ath6kl_err("%s(): lk_ahd mismatch! (pPkt:0x%p flags:0x%X)\n",
  1194. __func__, packet, packet->info.rx.rx_flags);
  1195. ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx expected lk_ahd",
  1196. "", &packet->info.rx.exp_hdr, 4);
  1197. ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx current header",
  1198. "", (u8 *)&lk_ahd, sizeof(lk_ahd));
  1199. status = -ENOMEM;
  1200. goto fail_rx;
  1201. }
  1202. if (htc_hdr->flags & HTC_FLG_RX_TRAILER) {
  1203. if (htc_hdr->ctrl[0] < sizeof(struct htc_record_hdr) ||
  1204. htc_hdr->ctrl[0] > payload_len) {
  1205. ath6kl_err("%s(): invalid hdr (payload len should be :%d, CB[0] is:%d)\n",
  1206. __func__, payload_len, htc_hdr->ctrl[0]);
  1207. status = -ENOMEM;
  1208. goto fail_rx;
  1209. }
  1210. if (packet->info.rx.rx_flags & HTC_RX_PKT_IGNORE_LOOKAHEAD) {
  1211. next_lkahds = NULL;
  1212. n_lkahds = NULL;
  1213. }
  1214. status = htc_proc_trailer(target, packet->buf + HTC_HDR_LENGTH
  1215. + payload_len - htc_hdr->ctrl[0],
  1216. htc_hdr->ctrl[0], next_lkahds,
  1217. n_lkahds, packet->endpoint);
  1218. if (status)
  1219. goto fail_rx;
  1220. packet->act_len -= htc_hdr->ctrl[0];
  1221. }
  1222. packet->buf += HTC_HDR_LENGTH;
  1223. packet->act_len -= HTC_HDR_LENGTH;
  1224. fail_rx:
  1225. if (status)
  1226. ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad packet",
  1227. "", packet->buf, packet->act_len);
  1228. return status;
  1229. }
  1230. static void ath6kl_htc_rx_complete(struct htc_endpoint *endpoint,
  1231. struct htc_packet *packet)
  1232. {
  1233. ath6kl_dbg(ATH6KL_DBG_HTC,
  1234. "htc rx complete ep %d packet 0x%p\n",
  1235. endpoint->eid, packet);
  1236. endpoint->ep_cb.rx(endpoint->target, packet);
  1237. }
  1238. static int ath6kl_htc_rx_bundle(struct htc_target *target,
  1239. struct list_head *rxq,
  1240. struct list_head *sync_compq,
  1241. int *n_pkt_fetched, bool part_bundle)
  1242. {
  1243. struct hif_scatter_req *scat_req;
  1244. struct htc_packet *packet;
  1245. int rem_space = target->max_rx_bndl_sz;
  1246. int n_scat_pkt, status = 0, i, len;
  1247. n_scat_pkt = get_queue_depth(rxq);
  1248. n_scat_pkt = min(n_scat_pkt, target->msg_per_bndl_max);
  1249. if ((get_queue_depth(rxq) - n_scat_pkt) > 0) {
  1250. /*
  1251. * We were forced to split this bundle receive operation
  1252. * all packets in this partial bundle must have their
  1253. * lookaheads ignored.
  1254. */
  1255. part_bundle = true;
  1256. /*
  1257. * This would only happen if the target ignored our max
  1258. * bundle limit.
  1259. */
  1260. ath6kl_warn("%s(): partial bundle detected num:%d , %d\n",
  1261. __func__, get_queue_depth(rxq), n_scat_pkt);
  1262. }
  1263. len = 0;
  1264. ath6kl_dbg(ATH6KL_DBG_HTC,
  1265. "htc rx bundle depth %d pkts %d\n",
  1266. get_queue_depth(rxq), n_scat_pkt);
  1267. scat_req = hif_scatter_req_get(target->dev->ar);
  1268. if (scat_req == NULL)
  1269. goto fail_rx_pkt;
  1270. for (i = 0; i < n_scat_pkt; i++) {
  1271. int pad_len;
  1272. packet = list_first_entry(rxq, struct htc_packet, list);
  1273. list_del(&packet->list);
  1274. pad_len = CALC_TXRX_PADDED_LEN(target,
  1275. packet->act_len);
  1276. if ((rem_space - pad_len) < 0) {
  1277. list_add(&packet->list, rxq);
  1278. break;
  1279. }
  1280. rem_space -= pad_len;
  1281. if (part_bundle || (i < (n_scat_pkt - 1)))
  1282. /*
  1283. * Packet 0..n-1 cannot be checked for look-aheads
  1284. * since we are fetching a bundle the last packet
  1285. * however can have it's lookahead used
  1286. */
  1287. packet->info.rx.rx_flags |=
  1288. HTC_RX_PKT_IGNORE_LOOKAHEAD;
  1289. /* NOTE: 1 HTC packet per scatter entry */
  1290. scat_req->scat_list[i].buf = packet->buf;
  1291. scat_req->scat_list[i].len = pad_len;
  1292. packet->info.rx.rx_flags |= HTC_RX_PKT_PART_OF_BUNDLE;
  1293. list_add_tail(&packet->list, sync_compq);
  1294. WARN_ON(!scat_req->scat_list[i].len);
  1295. len += scat_req->scat_list[i].len;
  1296. }
  1297. scat_req->len = len;
  1298. scat_req->scat_entries = i;
  1299. status = ath6kl_hif_submit_scat_req(target->dev, scat_req, true);
  1300. if (!status)
  1301. *n_pkt_fetched = i;
  1302. /* free scatter request */
  1303. hif_scatter_req_add(target->dev->ar, scat_req);
  1304. fail_rx_pkt:
  1305. return status;
  1306. }
  1307. static int ath6kl_htc_rx_process_packets(struct htc_target *target,
  1308. struct list_head *comp_pktq,
  1309. u32 lk_ahds[],
  1310. int *n_lk_ahd)
  1311. {
  1312. struct htc_packet *packet, *tmp_pkt;
  1313. struct htc_endpoint *ep;
  1314. int status = 0;
  1315. list_for_each_entry_safe(packet, tmp_pkt, comp_pktq, list) {
  1316. ep = &target->endpoint[packet->endpoint];
  1317. /* process header for each of the recv packet */
  1318. status = ath6kl_htc_rx_process_hdr(target, packet, lk_ahds,
  1319. n_lk_ahd);
  1320. if (status)
  1321. return status;
  1322. list_del(&packet->list);
  1323. if (list_empty(comp_pktq)) {
  1324. /*
  1325. * Last packet's more packet flag is set
  1326. * based on the lookahead.
  1327. */
  1328. if (*n_lk_ahd > 0)
  1329. ath6kl_htc_rx_set_indicate(lk_ahds[0],
  1330. ep, packet);
  1331. } else
  1332. /*
  1333. * Packets in a bundle automatically have
  1334. * this flag set.
  1335. */
  1336. packet->info.rx.indicat_flags |=
  1337. HTC_RX_FLAGS_INDICATE_MORE_PKTS;
  1338. ath6kl_htc_rx_update_stats(ep, *n_lk_ahd);
  1339. if (packet->info.rx.rx_flags & HTC_RX_PKT_PART_OF_BUNDLE)
  1340. ep->ep_st.rx_bundl += 1;
  1341. ath6kl_htc_rx_complete(ep, packet);
  1342. }
  1343. return status;
  1344. }
  1345. static int ath6kl_htc_rx_fetch(struct htc_target *target,
  1346. struct list_head *rx_pktq,
  1347. struct list_head *comp_pktq)
  1348. {
  1349. int fetched_pkts;
  1350. bool part_bundle = false;
  1351. int status = 0;
  1352. struct list_head tmp_rxq;
  1353. struct htc_packet *packet, *tmp_pkt;
  1354. /* now go fetch the list of HTC packets */
  1355. while (!list_empty(rx_pktq)) {
  1356. fetched_pkts = 0;
  1357. INIT_LIST_HEAD(&tmp_rxq);
  1358. if (target->rx_bndl_enable && (get_queue_depth(rx_pktq) > 1)) {
  1359. /*
  1360. * There are enough packets to attempt a
  1361. * bundle transfer and recv bundling is
  1362. * allowed.
  1363. */
  1364. status = ath6kl_htc_rx_bundle(target, rx_pktq,
  1365. &tmp_rxq,
  1366. &fetched_pkts,
  1367. part_bundle);
  1368. if (status)
  1369. goto fail_rx;
  1370. if (!list_empty(rx_pktq))
  1371. part_bundle = true;
  1372. list_splice_tail_init(&tmp_rxq, comp_pktq);
  1373. }
  1374. if (!fetched_pkts) {
  1375. packet = list_first_entry(rx_pktq, struct htc_packet,
  1376. list);
  1377. /* fully synchronous */
  1378. packet->completion = NULL;
  1379. if (!list_is_singular(rx_pktq))
  1380. /*
  1381. * look_aheads in all packet
  1382. * except the last one in the
  1383. * bundle must be ignored
  1384. */
  1385. packet->info.rx.rx_flags |=
  1386. HTC_RX_PKT_IGNORE_LOOKAHEAD;
  1387. /* go fetch the packet */
  1388. status = ath6kl_htc_rx_packet(target, packet,
  1389. packet->act_len);
  1390. list_move_tail(&packet->list, &tmp_rxq);
  1391. if (status)
  1392. goto fail_rx;
  1393. list_splice_tail_init(&tmp_rxq, comp_pktq);
  1394. }
  1395. }
  1396. return 0;
  1397. fail_rx:
  1398. /*
  1399. * Cleanup any packets we allocated but didn't use to
  1400. * actually fetch any packets.
  1401. */
  1402. list_for_each_entry_safe(packet, tmp_pkt, rx_pktq, list) {
  1403. list_del(&packet->list);
  1404. htc_reclaim_rxbuf(target, packet,
  1405. &target->endpoint[packet->endpoint]);
  1406. }
  1407. list_for_each_entry_safe(packet, tmp_pkt, &tmp_rxq, list) {
  1408. list_del(&packet->list);
  1409. htc_reclaim_rxbuf(target, packet,
  1410. &target->endpoint[packet->endpoint]);
  1411. }
  1412. return status;
  1413. }
  1414. int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
  1415. u32 msg_look_ahead, int *num_pkts)
  1416. {
  1417. struct htc_packet *packets, *tmp_pkt;
  1418. struct htc_endpoint *endpoint;
  1419. struct list_head rx_pktq, comp_pktq;
  1420. int status = 0;
  1421. u32 look_aheads[HTC_HOST_MAX_MSG_PER_BUNDLE];
  1422. int num_look_ahead = 1;
  1423. enum htc_endpoint_id id;
  1424. int n_fetched = 0;
  1425. *num_pkts = 0;
  1426. /*
  1427. * On first entry copy the look_aheads into our temp array for
  1428. * processing
  1429. */
  1430. look_aheads[0] = msg_look_ahead;
  1431. while (true) {
  1432. /*
  1433. * First lookahead sets the expected endpoint IDs for all
  1434. * packets in a bundle.
  1435. */
  1436. id = ((struct htc_frame_hdr *)&look_aheads[0])->eid;
  1437. endpoint = &target->endpoint[id];
  1438. if (id >= ENDPOINT_MAX) {
  1439. ath6kl_err("MsgPend, invalid endpoint in look-ahead: %d\n",
  1440. id);
  1441. status = -ENOMEM;
  1442. break;
  1443. }
  1444. INIT_LIST_HEAD(&rx_pktq);
  1445. INIT_LIST_HEAD(&comp_pktq);
  1446. /*
  1447. * Try to allocate as many HTC RX packets indicated by the
  1448. * look_aheads.
  1449. */
  1450. status = ath6kl_htc_rx_alloc(target, look_aheads,
  1451. num_look_ahead, endpoint,
  1452. &rx_pktq);
  1453. if (status)
  1454. break;
  1455. if (get_queue_depth(&rx_pktq) >= 2)
  1456. /*
  1457. * A recv bundle was detected, force IRQ status
  1458. * re-check again
  1459. */
  1460. target->chk_irq_status_cnt = 1;
  1461. n_fetched += get_queue_depth(&rx_pktq);
  1462. num_look_ahead = 0;
  1463. status = ath6kl_htc_rx_fetch(target, &rx_pktq, &comp_pktq);
  1464. if (!status)
  1465. ath6kl_htc_rx_chk_water_mark(endpoint);
  1466. /* Process fetched packets */
  1467. status = ath6kl_htc_rx_process_packets(target, &comp_pktq,
  1468. look_aheads,
  1469. &num_look_ahead);
  1470. if (!num_look_ahead || status)
  1471. break;
  1472. /*
  1473. * For SYNCH processing, if we get here, we are running
  1474. * through the loop again due to a detected lookahead. Set
  1475. * flag that we should re-check IRQ status registers again
  1476. * before leaving IRQ processing, this can net better
  1477. * performance in high throughput situations.
  1478. */
  1479. target->chk_irq_status_cnt = 1;
  1480. }
  1481. if (status) {
  1482. ath6kl_err("failed to get pending recv messages: %d\n",
  1483. status);
  1484. /* cleanup any packets in sync completion queue */
  1485. list_for_each_entry_safe(packets, tmp_pkt, &comp_pktq, list) {
  1486. list_del(&packets->list);
  1487. htc_reclaim_rxbuf(target, packets,
  1488. &target->endpoint[packets->endpoint]);
  1489. }
  1490. if (target->htc_flags & HTC_OP_STATE_STOPPING) {
  1491. ath6kl_warn("host is going to stop blocking receiver for htc_stop\n");
  1492. ath6kl_hif_rx_control(target->dev, false);
  1493. }
  1494. }
  1495. /*
  1496. * Before leaving, check to see if host ran out of buffers and
  1497. * needs to stop the receiver.
  1498. */
  1499. if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
  1500. ath6kl_warn("host has no rx buffers blocking receiver to prevent overrun\n");
  1501. ath6kl_hif_rx_control(target->dev, false);
  1502. }
  1503. *num_pkts = n_fetched;
  1504. return status;
  1505. }
  1506. /*
  1507. * Synchronously wait for a control message from the target,
  1508. * This function is used at initialization time ONLY. At init messages
  1509. * on ENDPOINT 0 are expected.
  1510. */
  1511. static struct htc_packet *htc_wait_for_ctrl_msg(struct htc_target *target)
  1512. {
  1513. struct htc_packet *packet = NULL;
  1514. struct htc_frame_hdr *htc_hdr;
  1515. u32 look_ahead;
  1516. if (ath6kl_hif_poll_mboxmsg_rx(target->dev, &look_ahead,
  1517. HTC_TARGET_RESPONSE_TIMEOUT))
  1518. return NULL;
  1519. ath6kl_dbg(ATH6KL_DBG_HTC,
  1520. "htc rx wait ctrl look_ahead 0x%X\n", look_ahead);
  1521. htc_hdr = (struct htc_frame_hdr *)&look_ahead;
  1522. if (htc_hdr->eid != ENDPOINT_0)
  1523. return NULL;
  1524. packet = htc_get_control_buf(target, false);
  1525. if (!packet)
  1526. return NULL;
  1527. packet->info.rx.rx_flags = 0;
  1528. packet->info.rx.exp_hdr = look_ahead;
  1529. packet->act_len = le16_to_cpu(htc_hdr->payld_len) + HTC_HDR_LENGTH;
  1530. if (packet->act_len > packet->buf_len)
  1531. goto fail_ctrl_rx;
  1532. /* we want synchronous operation */
  1533. packet->completion = NULL;
  1534. /* get the message from the device, this will block */
  1535. if (ath6kl_htc_rx_packet(target, packet, packet->act_len))
  1536. goto fail_ctrl_rx;
  1537. /* process receive header */
  1538. packet->status = ath6kl_htc_rx_process_hdr(target, packet, NULL, NULL);
  1539. if (packet->status) {
  1540. ath6kl_err("htc_wait_for_ctrl_msg, ath6kl_htc_rx_process_hdr failed (status = %d)\n",
  1541. packet->status);
  1542. goto fail_ctrl_rx;
  1543. }
  1544. return packet;
  1545. fail_ctrl_rx:
  1546. if (packet != NULL) {
  1547. htc_rxpkt_reset(packet);
  1548. reclaim_rx_ctrl_buf(target, packet);
  1549. }
  1550. return NULL;
  1551. }
  1552. int ath6kl_htc_add_rxbuf_multiple(struct htc_target *target,
  1553. struct list_head *pkt_queue)
  1554. {
  1555. struct htc_endpoint *endpoint;
  1556. struct htc_packet *first_pkt;
  1557. bool rx_unblock = false;
  1558. int status = 0, depth;
  1559. if (list_empty(pkt_queue))
  1560. return -ENOMEM;
  1561. first_pkt = list_first_entry(pkt_queue, struct htc_packet, list);
  1562. if (first_pkt->endpoint >= ENDPOINT_MAX)
  1563. return status;
  1564. depth = get_queue_depth(pkt_queue);
  1565. ath6kl_dbg(ATH6KL_DBG_HTC,
  1566. "htc rx add multiple ep id %d cnt %d len %d\n",
  1567. first_pkt->endpoint, depth, first_pkt->buf_len);
  1568. endpoint = &target->endpoint[first_pkt->endpoint];
  1569. if (target->htc_flags & HTC_OP_STATE_STOPPING) {
  1570. struct htc_packet *packet, *tmp_pkt;
  1571. /* walk through queue and mark each one canceled */
  1572. list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
  1573. packet->status = -ECANCELED;
  1574. list_del(&packet->list);
  1575. ath6kl_htc_rx_complete(endpoint, packet);
  1576. }
  1577. return status;
  1578. }
  1579. spin_lock_bh(&target->rx_lock);
  1580. list_splice_tail_init(pkt_queue, &endpoint->rx_bufq);
  1581. /* check if we are blocked waiting for a new buffer */
  1582. if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
  1583. if (target->ep_waiting == first_pkt->endpoint) {
  1584. ath6kl_dbg(ATH6KL_DBG_HTC,
  1585. "htc rx blocked on ep %d, unblocking\n",
  1586. target->ep_waiting);
  1587. target->rx_st_flags &= ~HTC_RECV_WAIT_BUFFERS;
  1588. target->ep_waiting = ENDPOINT_MAX;
  1589. rx_unblock = true;
  1590. }
  1591. }
  1592. spin_unlock_bh(&target->rx_lock);
  1593. if (rx_unblock && !(target->htc_flags & HTC_OP_STATE_STOPPING))
  1594. /* TODO : implement a buffer threshold count? */
  1595. ath6kl_hif_rx_control(target->dev, true);
  1596. return status;
  1597. }
  1598. void ath6kl_htc_flush_rx_buf(struct htc_target *target)
  1599. {
  1600. struct htc_endpoint *endpoint;
  1601. struct htc_packet *packet, *tmp_pkt;
  1602. int i;
  1603. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  1604. endpoint = &target->endpoint[i];
  1605. if (!endpoint->svc_id)
  1606. /* not in use.. */
  1607. continue;
  1608. spin_lock_bh(&target->rx_lock);
  1609. list_for_each_entry_safe(packet, tmp_pkt,
  1610. &endpoint->rx_bufq, list) {
  1611. list_del(&packet->list);
  1612. spin_unlock_bh(&target->rx_lock);
  1613. ath6kl_dbg(ATH6KL_DBG_HTC,
  1614. "htc rx flush pkt 0x%p len %d ep %d\n",
  1615. packet, packet->buf_len,
  1616. packet->endpoint);
  1617. dev_kfree_skb(packet->pkt_cntxt);
  1618. spin_lock_bh(&target->rx_lock);
  1619. }
  1620. spin_unlock_bh(&target->rx_lock);
  1621. }
  1622. }
  1623. int ath6kl_htc_conn_service(struct htc_target *target,
  1624. struct htc_service_connect_req *conn_req,
  1625. struct htc_service_connect_resp *conn_resp)
  1626. {
  1627. struct htc_packet *rx_pkt = NULL;
  1628. struct htc_packet *tx_pkt = NULL;
  1629. struct htc_conn_service_resp *resp_msg;
  1630. struct htc_conn_service_msg *conn_msg;
  1631. struct htc_endpoint *endpoint;
  1632. enum htc_endpoint_id assigned_ep = ENDPOINT_MAX;
  1633. unsigned int max_msg_sz = 0;
  1634. int status = 0;
  1635. ath6kl_dbg(ATH6KL_DBG_HTC,
  1636. "htc connect service target 0x%p service id 0x%x\n",
  1637. target, conn_req->svc_id);
  1638. if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
  1639. /* special case for pseudo control service */
  1640. assigned_ep = ENDPOINT_0;
  1641. max_msg_sz = HTC_MAX_CTRL_MSG_LEN;
  1642. } else {
  1643. /* allocate a packet to send to the target */
  1644. tx_pkt = htc_get_control_buf(target, true);
  1645. if (!tx_pkt)
  1646. return -ENOMEM;
  1647. conn_msg = (struct htc_conn_service_msg *)tx_pkt->buf;
  1648. memset(conn_msg, 0, sizeof(*conn_msg));
  1649. conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
  1650. conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
  1651. conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags);
  1652. set_htc_pkt_info(tx_pkt, NULL, (u8 *) conn_msg,
  1653. sizeof(*conn_msg) + conn_msg->svc_meta_len,
  1654. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  1655. /* we want synchronous operation */
  1656. tx_pkt->completion = NULL;
  1657. ath6kl_htc_tx_prep_pkt(tx_pkt, 0, 0, 0);
  1658. status = ath6kl_htc_tx_issue(target, tx_pkt);
  1659. if (status)
  1660. goto fail_tx;
  1661. /* wait for response */
  1662. rx_pkt = htc_wait_for_ctrl_msg(target);
  1663. if (!rx_pkt) {
  1664. status = -ENOMEM;
  1665. goto fail_tx;
  1666. }
  1667. resp_msg = (struct htc_conn_service_resp *)rx_pkt->buf;
  1668. if ((le16_to_cpu(resp_msg->msg_id) != HTC_MSG_CONN_SVC_RESP_ID)
  1669. || (rx_pkt->act_len < sizeof(*resp_msg))) {
  1670. status = -ENOMEM;
  1671. goto fail_tx;
  1672. }
  1673. conn_resp->resp_code = resp_msg->status;
  1674. /* check response status */
  1675. if (resp_msg->status != HTC_SERVICE_SUCCESS) {
  1676. ath6kl_err("target failed service 0x%X connect request (status:%d)\n",
  1677. resp_msg->svc_id, resp_msg->status);
  1678. status = -ENOMEM;
  1679. goto fail_tx;
  1680. }
  1681. assigned_ep = (enum htc_endpoint_id)resp_msg->eid;
  1682. max_msg_sz = le16_to_cpu(resp_msg->max_msg_sz);
  1683. }
  1684. if (assigned_ep >= ENDPOINT_MAX || !max_msg_sz) {
  1685. status = -ENOMEM;
  1686. goto fail_tx;
  1687. }
  1688. endpoint = &target->endpoint[assigned_ep];
  1689. endpoint->eid = assigned_ep;
  1690. if (endpoint->svc_id) {
  1691. status = -ENOMEM;
  1692. goto fail_tx;
  1693. }
  1694. /* return assigned endpoint to caller */
  1695. conn_resp->endpoint = assigned_ep;
  1696. conn_resp->len_max = max_msg_sz;
  1697. /* setup the endpoint */
  1698. /* this marks the endpoint in use */
  1699. endpoint->svc_id = conn_req->svc_id;
  1700. endpoint->max_txq_depth = conn_req->max_txq_depth;
  1701. endpoint->len_max = max_msg_sz;
  1702. endpoint->ep_cb = conn_req->ep_cb;
  1703. endpoint->cred_dist.svc_id = conn_req->svc_id;
  1704. endpoint->cred_dist.htc_rsvd = endpoint;
  1705. endpoint->cred_dist.endpoint = assigned_ep;
  1706. endpoint->cred_dist.cred_sz = target->tgt_cred_sz;
  1707. if (conn_req->max_rxmsg_sz) {
  1708. /*
  1709. * Override cred_per_msg calculation, this optimizes
  1710. * the credit-low indications since the host will actually
  1711. * issue smaller messages in the Send path.
  1712. */
  1713. if (conn_req->max_rxmsg_sz > max_msg_sz) {
  1714. status = -ENOMEM;
  1715. goto fail_tx;
  1716. }
  1717. endpoint->cred_dist.cred_per_msg =
  1718. conn_req->max_rxmsg_sz / target->tgt_cred_sz;
  1719. } else
  1720. endpoint->cred_dist.cred_per_msg =
  1721. max_msg_sz / target->tgt_cred_sz;
  1722. if (!endpoint->cred_dist.cred_per_msg)
  1723. endpoint->cred_dist.cred_per_msg = 1;
  1724. /* save local connection flags */
  1725. endpoint->conn_flags = conn_req->flags;
  1726. fail_tx:
  1727. if (tx_pkt)
  1728. htc_reclaim_txctrl_buf(target, tx_pkt);
  1729. if (rx_pkt) {
  1730. htc_rxpkt_reset(rx_pkt);
  1731. reclaim_rx_ctrl_buf(target, rx_pkt);
  1732. }
  1733. return status;
  1734. }
  1735. static void reset_ep_state(struct htc_target *target)
  1736. {
  1737. struct htc_endpoint *endpoint;
  1738. int i;
  1739. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  1740. endpoint = &target->endpoint[i];
  1741. memset(&endpoint->cred_dist, 0, sizeof(endpoint->cred_dist));
  1742. endpoint->svc_id = 0;
  1743. endpoint->len_max = 0;
  1744. endpoint->max_txq_depth = 0;
  1745. memset(&endpoint->ep_st, 0,
  1746. sizeof(endpoint->ep_st));
  1747. INIT_LIST_HEAD(&endpoint->rx_bufq);
  1748. INIT_LIST_HEAD(&endpoint->txq);
  1749. endpoint->target = target;
  1750. }
  1751. /* reset distribution list */
  1752. INIT_LIST_HEAD(&target->cred_dist_list);
  1753. }
  1754. int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
  1755. enum htc_endpoint_id endpoint)
  1756. {
  1757. int num;
  1758. spin_lock_bh(&target->rx_lock);
  1759. num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
  1760. spin_unlock_bh(&target->rx_lock);
  1761. return num;
  1762. }
  1763. static void htc_setup_msg_bndl(struct htc_target *target)
  1764. {
  1765. /* limit what HTC can handle */
  1766. target->msg_per_bndl_max = min(HTC_HOST_MAX_MSG_PER_BUNDLE,
  1767. target->msg_per_bndl_max);
  1768. if (ath6kl_hif_enable_scatter(target->dev->ar)) {
  1769. target->msg_per_bndl_max = 0;
  1770. return;
  1771. }
  1772. /* limit bundle what the device layer can handle */
  1773. target->msg_per_bndl_max = min(target->max_scat_entries,
  1774. target->msg_per_bndl_max);
  1775. ath6kl_dbg(ATH6KL_DBG_HTC,
  1776. "htc bundling allowed msg_per_bndl_max %d\n",
  1777. target->msg_per_bndl_max);
  1778. /* Max rx bundle size is limited by the max tx bundle size */
  1779. target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
  1780. /* Max tx bundle size if limited by the extended mbox address range */
  1781. target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
  1782. target->max_xfer_szper_scatreq);
  1783. ath6kl_dbg(ATH6KL_DBG_HTC, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
  1784. target->max_rx_bndl_sz, target->max_tx_bndl_sz);
  1785. if (target->max_tx_bndl_sz)
  1786. target->tx_bndl_enable = true;
  1787. if (target->max_rx_bndl_sz)
  1788. target->rx_bndl_enable = true;
  1789. if ((target->tgt_cred_sz % target->block_sz) != 0) {
  1790. ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
  1791. target->tgt_cred_sz);
  1792. /*
  1793. * Disallow send bundling since the credit size is
  1794. * not aligned to a block size the I/O block
  1795. * padding will spill into the next credit buffer
  1796. * which is fatal.
  1797. */
  1798. target->tx_bndl_enable = false;
  1799. }
  1800. }
  1801. int ath6kl_htc_wait_target(struct htc_target *target)
  1802. {
  1803. struct htc_packet *packet = NULL;
  1804. struct htc_ready_ext_msg *rdy_msg;
  1805. struct htc_service_connect_req connect;
  1806. struct htc_service_connect_resp resp;
  1807. int status;
  1808. /* we should be getting 1 control message that the target is ready */
  1809. packet = htc_wait_for_ctrl_msg(target);
  1810. if (!packet)
  1811. return -ENOMEM;
  1812. /* we controlled the buffer creation so it's properly aligned */
  1813. rdy_msg = (struct htc_ready_ext_msg *)packet->buf;
  1814. if ((le16_to_cpu(rdy_msg->ver2_0_info.msg_id) != HTC_MSG_READY_ID) ||
  1815. (packet->act_len < sizeof(struct htc_ready_msg))) {
  1816. status = -ENOMEM;
  1817. goto fail_wait_target;
  1818. }
  1819. if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
  1820. status = -ENOMEM;
  1821. goto fail_wait_target;
  1822. }
  1823. target->tgt_creds = le16_to_cpu(rdy_msg->ver2_0_info.cred_cnt);
  1824. target->tgt_cred_sz = le16_to_cpu(rdy_msg->ver2_0_info.cred_sz);
  1825. ath6kl_dbg(ATH6KL_DBG_HTC,
  1826. "htc target ready credits %d size %d\n",
  1827. target->tgt_creds, target->tgt_cred_sz);
  1828. /* check if this is an extended ready message */
  1829. if (packet->act_len >= sizeof(struct htc_ready_ext_msg)) {
  1830. /* this is an extended message */
  1831. target->htc_tgt_ver = rdy_msg->htc_ver;
  1832. target->msg_per_bndl_max = rdy_msg->msg_per_htc_bndl;
  1833. } else {
  1834. /* legacy */
  1835. target->htc_tgt_ver = HTC_VERSION_2P0;
  1836. target->msg_per_bndl_max = 0;
  1837. }
  1838. ath6kl_dbg(ATH6KL_DBG_HTC, "htc using protocol %s (%d)\n",
  1839. (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
  1840. target->htc_tgt_ver);
  1841. if (target->msg_per_bndl_max > 0)
  1842. htc_setup_msg_bndl(target);
  1843. /* setup our pseudo HTC control endpoint connection */
  1844. memset(&connect, 0, sizeof(connect));
  1845. memset(&resp, 0, sizeof(resp));
  1846. connect.ep_cb.rx = htc_ctrl_rx;
  1847. connect.ep_cb.rx_refill = NULL;
  1848. connect.ep_cb.tx_full = NULL;
  1849. connect.max_txq_depth = NUM_CONTROL_BUFFERS;
  1850. connect.svc_id = HTC_CTRL_RSVD_SVC;
  1851. /* connect fake service */
  1852. status = ath6kl_htc_conn_service((void *)target, &connect, &resp);
  1853. if (status)
  1854. ath6kl_hif_cleanup_scatter(target->dev->ar);
  1855. fail_wait_target:
  1856. if (packet) {
  1857. htc_rxpkt_reset(packet);
  1858. reclaim_rx_ctrl_buf(target, packet);
  1859. }
  1860. return status;
  1861. }
  1862. /*
  1863. * Start HTC, enable interrupts and let the target know
  1864. * host has finished setup.
  1865. */
  1866. int ath6kl_htc_start(struct htc_target *target)
  1867. {
  1868. struct htc_packet *packet;
  1869. int status;
  1870. /* Disable interrupts at the chip level */
  1871. ath6kl_hif_disable_intrs(target->dev);
  1872. target->htc_flags = 0;
  1873. target->rx_st_flags = 0;
  1874. /* Push control receive buffers into htc control endpoint */
  1875. while ((packet = htc_get_control_buf(target, false)) != NULL) {
  1876. status = htc_add_rxbuf(target, packet);
  1877. if (status)
  1878. return status;
  1879. }
  1880. /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
  1881. ath6k_credit_init(target->cred_dist_cntxt, &target->cred_dist_list,
  1882. target->tgt_creds);
  1883. dump_cred_dist_stats(target);
  1884. /* Indicate to the target of the setup completion */
  1885. status = htc_setup_tx_complete(target);
  1886. if (status)
  1887. return status;
  1888. /* unmask interrupts */
  1889. status = ath6kl_hif_unmask_intrs(target->dev);
  1890. if (status)
  1891. ath6kl_htc_stop(target);
  1892. return status;
  1893. }
  1894. /* htc_stop: stop interrupt reception, and flush all queued buffers */
  1895. void ath6kl_htc_stop(struct htc_target *target)
  1896. {
  1897. spin_lock_bh(&target->htc_lock);
  1898. target->htc_flags |= HTC_OP_STATE_STOPPING;
  1899. spin_unlock_bh(&target->htc_lock);
  1900. /*
  1901. * Masking interrupts is a synchronous operation, when this
  1902. * function returns all pending HIF I/O has completed, we can
  1903. * safely flush the queues.
  1904. */
  1905. ath6kl_hif_mask_intrs(target->dev);
  1906. ath6kl_htc_flush_txep_all(target);
  1907. ath6kl_htc_flush_rx_buf(target);
  1908. reset_ep_state(target);
  1909. }
  1910. void *ath6kl_htc_create(struct ath6kl *ar)
  1911. {
  1912. struct htc_target *target = NULL;
  1913. struct htc_packet *packet;
  1914. int status = 0, i = 0;
  1915. u32 block_size, ctrl_bufsz;
  1916. target = kzalloc(sizeof(*target), GFP_KERNEL);
  1917. if (!target) {
  1918. ath6kl_err("unable to allocate memory\n");
  1919. return NULL;
  1920. }
  1921. target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
  1922. if (!target->dev) {
  1923. ath6kl_err("unable to allocate memory\n");
  1924. status = -ENOMEM;
  1925. goto fail_create_htc;
  1926. }
  1927. spin_lock_init(&target->htc_lock);
  1928. spin_lock_init(&target->rx_lock);
  1929. spin_lock_init(&target->tx_lock);
  1930. INIT_LIST_HEAD(&target->free_ctrl_txbuf);
  1931. INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
  1932. INIT_LIST_HEAD(&target->cred_dist_list);
  1933. target->dev->ar = ar;
  1934. target->dev->htc_cnxt = target;
  1935. target->ep_waiting = ENDPOINT_MAX;
  1936. reset_ep_state(target);
  1937. status = ath6kl_hif_setup(target->dev);
  1938. if (status)
  1939. goto fail_create_htc;
  1940. block_size = ar->mbox_info.block_size;
  1941. ctrl_bufsz = (block_size > HTC_MAX_CTRL_MSG_LEN) ?
  1942. (block_size + HTC_HDR_LENGTH) :
  1943. (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH);
  1944. for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
  1945. packet = kzalloc(sizeof(*packet), GFP_KERNEL);
  1946. if (!packet)
  1947. break;
  1948. packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
  1949. if (!packet->buf_start) {
  1950. kfree(packet);
  1951. break;
  1952. }
  1953. packet->buf_len = ctrl_bufsz;
  1954. if (i < NUM_CONTROL_RX_BUFFERS) {
  1955. packet->act_len = 0;
  1956. packet->buf = packet->buf_start;
  1957. packet->endpoint = ENDPOINT_0;
  1958. list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
  1959. } else
  1960. list_add_tail(&packet->list, &target->free_ctrl_txbuf);
  1961. }
  1962. fail_create_htc:
  1963. if (i != NUM_CONTROL_BUFFERS || status) {
  1964. if (target) {
  1965. ath6kl_htc_cleanup(target);
  1966. target = NULL;
  1967. }
  1968. }
  1969. return target;
  1970. }
  1971. /* cleanup the HTC instance */
  1972. void ath6kl_htc_cleanup(struct htc_target *target)
  1973. {
  1974. struct htc_packet *packet, *tmp_packet;
  1975. ath6kl_hif_cleanup_scatter(target->dev->ar);
  1976. list_for_each_entry_safe(packet, tmp_packet,
  1977. &target->free_ctrl_txbuf, list) {
  1978. list_del(&packet->list);
  1979. kfree(packet->buf_start);
  1980. kfree(packet);
  1981. }
  1982. list_for_each_entry_safe(packet, tmp_packet,
  1983. &target->free_ctrl_rxbuf, list) {
  1984. list_del(&packet->list);
  1985. kfree(packet->buf_start);
  1986. kfree(packet);
  1987. }
  1988. kfree(target->dev);
  1989. kfree(target);
  1990. }