htc.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  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->credit_info, &target->cred_dist_list);
  87. ath6kl_credit_distribute(target->credit_info,
  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->credit_info, &ep->cred_dist);
  189. ath6kl_seek_credits(target->credit_info, &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->credit_info, &ep->cred_dist);
  206. ath6kl_seek_credits(target->credit_info, &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 = cred_dist->htc_ep;
  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 ath6kl_htc_credit_info *credit_info,
  559. u16 srvc_pri_order[], int list_len)
  560. {
  561. struct htc_endpoint *endpoint;
  562. int i, ep;
  563. target->credit_info = credit_info;
  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->credit_info, &target->cred_dist_list);
  674. ath6kl_credit_distribute(target->credit_info,
  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->credit_info, &target->cred_dist_list);
  1031. ath6kl_credit_distribute(target->credit_info,
  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_ep = 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. /* FIXME: free existing entries */
  1753. INIT_LIST_HEAD(&target->cred_dist_list);
  1754. }
  1755. int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
  1756. enum htc_endpoint_id endpoint)
  1757. {
  1758. int num;
  1759. spin_lock_bh(&target->rx_lock);
  1760. num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
  1761. spin_unlock_bh(&target->rx_lock);
  1762. return num;
  1763. }
  1764. static void htc_setup_msg_bndl(struct htc_target *target)
  1765. {
  1766. /* limit what HTC can handle */
  1767. target->msg_per_bndl_max = min(HTC_HOST_MAX_MSG_PER_BUNDLE,
  1768. target->msg_per_bndl_max);
  1769. if (ath6kl_hif_enable_scatter(target->dev->ar)) {
  1770. target->msg_per_bndl_max = 0;
  1771. return;
  1772. }
  1773. /* limit bundle what the device layer can handle */
  1774. target->msg_per_bndl_max = min(target->max_scat_entries,
  1775. target->msg_per_bndl_max);
  1776. ath6kl_dbg(ATH6KL_DBG_HTC,
  1777. "htc bundling allowed msg_per_bndl_max %d\n",
  1778. target->msg_per_bndl_max);
  1779. /* Max rx bundle size is limited by the max tx bundle size */
  1780. target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
  1781. /* Max tx bundle size if limited by the extended mbox address range */
  1782. target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
  1783. target->max_xfer_szper_scatreq);
  1784. ath6kl_dbg(ATH6KL_DBG_HTC, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
  1785. target->max_rx_bndl_sz, target->max_tx_bndl_sz);
  1786. if (target->max_tx_bndl_sz)
  1787. target->tx_bndl_enable = true;
  1788. if (target->max_rx_bndl_sz)
  1789. target->rx_bndl_enable = true;
  1790. if ((target->tgt_cred_sz % target->block_sz) != 0) {
  1791. ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
  1792. target->tgt_cred_sz);
  1793. /*
  1794. * Disallow send bundling since the credit size is
  1795. * not aligned to a block size the I/O block
  1796. * padding will spill into the next credit buffer
  1797. * which is fatal.
  1798. */
  1799. target->tx_bndl_enable = false;
  1800. }
  1801. }
  1802. int ath6kl_htc_wait_target(struct htc_target *target)
  1803. {
  1804. struct htc_packet *packet = NULL;
  1805. struct htc_ready_ext_msg *rdy_msg;
  1806. struct htc_service_connect_req connect;
  1807. struct htc_service_connect_resp resp;
  1808. int status;
  1809. /* we should be getting 1 control message that the target is ready */
  1810. packet = htc_wait_for_ctrl_msg(target);
  1811. if (!packet)
  1812. return -ENOMEM;
  1813. /* we controlled the buffer creation so it's properly aligned */
  1814. rdy_msg = (struct htc_ready_ext_msg *)packet->buf;
  1815. if ((le16_to_cpu(rdy_msg->ver2_0_info.msg_id) != HTC_MSG_READY_ID) ||
  1816. (packet->act_len < sizeof(struct htc_ready_msg))) {
  1817. status = -ENOMEM;
  1818. goto fail_wait_target;
  1819. }
  1820. if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
  1821. status = -ENOMEM;
  1822. goto fail_wait_target;
  1823. }
  1824. target->tgt_creds = le16_to_cpu(rdy_msg->ver2_0_info.cred_cnt);
  1825. target->tgt_cred_sz = le16_to_cpu(rdy_msg->ver2_0_info.cred_sz);
  1826. ath6kl_dbg(ATH6KL_DBG_HTC,
  1827. "htc target ready credits %d size %d\n",
  1828. target->tgt_creds, target->tgt_cred_sz);
  1829. /* check if this is an extended ready message */
  1830. if (packet->act_len >= sizeof(struct htc_ready_ext_msg)) {
  1831. /* this is an extended message */
  1832. target->htc_tgt_ver = rdy_msg->htc_ver;
  1833. target->msg_per_bndl_max = rdy_msg->msg_per_htc_bndl;
  1834. } else {
  1835. /* legacy */
  1836. target->htc_tgt_ver = HTC_VERSION_2P0;
  1837. target->msg_per_bndl_max = 0;
  1838. }
  1839. ath6kl_dbg(ATH6KL_DBG_HTC, "htc using protocol %s (%d)\n",
  1840. (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
  1841. target->htc_tgt_ver);
  1842. if (target->msg_per_bndl_max > 0)
  1843. htc_setup_msg_bndl(target);
  1844. /* setup our pseudo HTC control endpoint connection */
  1845. memset(&connect, 0, sizeof(connect));
  1846. memset(&resp, 0, sizeof(resp));
  1847. connect.ep_cb.rx = htc_ctrl_rx;
  1848. connect.ep_cb.rx_refill = NULL;
  1849. connect.ep_cb.tx_full = NULL;
  1850. connect.max_txq_depth = NUM_CONTROL_BUFFERS;
  1851. connect.svc_id = HTC_CTRL_RSVD_SVC;
  1852. /* connect fake service */
  1853. status = ath6kl_htc_conn_service((void *)target, &connect, &resp);
  1854. if (status)
  1855. ath6kl_hif_cleanup_scatter(target->dev->ar);
  1856. fail_wait_target:
  1857. if (packet) {
  1858. htc_rxpkt_reset(packet);
  1859. reclaim_rx_ctrl_buf(target, packet);
  1860. }
  1861. return status;
  1862. }
  1863. /*
  1864. * Start HTC, enable interrupts and let the target know
  1865. * host has finished setup.
  1866. */
  1867. int ath6kl_htc_start(struct htc_target *target)
  1868. {
  1869. struct htc_packet *packet;
  1870. int status;
  1871. /* Disable interrupts at the chip level */
  1872. ath6kl_hif_disable_intrs(target->dev);
  1873. target->htc_flags = 0;
  1874. target->rx_st_flags = 0;
  1875. /* Push control receive buffers into htc control endpoint */
  1876. while ((packet = htc_get_control_buf(target, false)) != NULL) {
  1877. status = htc_add_rxbuf(target, packet);
  1878. if (status)
  1879. return status;
  1880. }
  1881. /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
  1882. ath6kl_credit_init(target->credit_info, &target->cred_dist_list,
  1883. target->tgt_creds);
  1884. dump_cred_dist_stats(target);
  1885. /* Indicate to the target of the setup completion */
  1886. status = htc_setup_tx_complete(target);
  1887. if (status)
  1888. return status;
  1889. /* unmask interrupts */
  1890. status = ath6kl_hif_unmask_intrs(target->dev);
  1891. if (status)
  1892. ath6kl_htc_stop(target);
  1893. return status;
  1894. }
  1895. /* htc_stop: stop interrupt reception, and flush all queued buffers */
  1896. void ath6kl_htc_stop(struct htc_target *target)
  1897. {
  1898. spin_lock_bh(&target->htc_lock);
  1899. target->htc_flags |= HTC_OP_STATE_STOPPING;
  1900. spin_unlock_bh(&target->htc_lock);
  1901. /*
  1902. * Masking interrupts is a synchronous operation, when this
  1903. * function returns all pending HIF I/O has completed, we can
  1904. * safely flush the queues.
  1905. */
  1906. ath6kl_hif_mask_intrs(target->dev);
  1907. ath6kl_htc_flush_txep_all(target);
  1908. ath6kl_htc_flush_rx_buf(target);
  1909. reset_ep_state(target);
  1910. }
  1911. void *ath6kl_htc_create(struct ath6kl *ar)
  1912. {
  1913. struct htc_target *target = NULL;
  1914. struct htc_packet *packet;
  1915. int status = 0, i = 0;
  1916. u32 block_size, ctrl_bufsz;
  1917. target = kzalloc(sizeof(*target), GFP_KERNEL);
  1918. if (!target) {
  1919. ath6kl_err("unable to allocate memory\n");
  1920. return NULL;
  1921. }
  1922. target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
  1923. if (!target->dev) {
  1924. ath6kl_err("unable to allocate memory\n");
  1925. status = -ENOMEM;
  1926. goto fail_create_htc;
  1927. }
  1928. spin_lock_init(&target->htc_lock);
  1929. spin_lock_init(&target->rx_lock);
  1930. spin_lock_init(&target->tx_lock);
  1931. INIT_LIST_HEAD(&target->free_ctrl_txbuf);
  1932. INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
  1933. INIT_LIST_HEAD(&target->cred_dist_list);
  1934. target->dev->ar = ar;
  1935. target->dev->htc_cnxt = target;
  1936. target->ep_waiting = ENDPOINT_MAX;
  1937. reset_ep_state(target);
  1938. status = ath6kl_hif_setup(target->dev);
  1939. if (status)
  1940. goto fail_create_htc;
  1941. block_size = ar->mbox_info.block_size;
  1942. ctrl_bufsz = (block_size > HTC_MAX_CTRL_MSG_LEN) ?
  1943. (block_size + HTC_HDR_LENGTH) :
  1944. (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH);
  1945. for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
  1946. packet = kzalloc(sizeof(*packet), GFP_KERNEL);
  1947. if (!packet)
  1948. break;
  1949. packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
  1950. if (!packet->buf_start) {
  1951. kfree(packet);
  1952. break;
  1953. }
  1954. packet->buf_len = ctrl_bufsz;
  1955. if (i < NUM_CONTROL_RX_BUFFERS) {
  1956. packet->act_len = 0;
  1957. packet->buf = packet->buf_start;
  1958. packet->endpoint = ENDPOINT_0;
  1959. list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
  1960. } else
  1961. list_add_tail(&packet->list, &target->free_ctrl_txbuf);
  1962. }
  1963. fail_create_htc:
  1964. if (i != NUM_CONTROL_BUFFERS || status) {
  1965. if (target) {
  1966. ath6kl_htc_cleanup(target);
  1967. target = NULL;
  1968. }
  1969. }
  1970. return target;
  1971. }
  1972. /* cleanup the HTC instance */
  1973. void ath6kl_htc_cleanup(struct htc_target *target)
  1974. {
  1975. struct htc_packet *packet, *tmp_packet;
  1976. ath6kl_hif_cleanup_scatter(target->dev->ar);
  1977. list_for_each_entry_safe(packet, tmp_packet,
  1978. &target->free_ctrl_txbuf, list) {
  1979. list_del(&packet->list);
  1980. kfree(packet->buf_start);
  1981. kfree(packet);
  1982. }
  1983. list_for_each_entry_safe(packet, tmp_packet,
  1984. &target->free_ctrl_rxbuf, list) {
  1985. list_del(&packet->list);
  1986. kfree(packet->buf_start);
  1987. kfree(packet);
  1988. }
  1989. kfree(target->dev);
  1990. kfree(target);
  1991. }