htc.c 62 KB

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