htc.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  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 "htc_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_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 htc_prep_send_pkt(struct htc_packet *packet, u8 flags, int ctrl0,
  32. 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 htc_issue_send(struct htc_target *target, struct htc_packet *packet)
  145. {
  146. int status;
  147. bool sync = false;
  148. u32 padded_len, send_len;
  149. if (!packet->completion)
  150. sync = true;
  151. send_len = packet->act_len + HTC_HDR_LENGTH;
  152. ath6kl_dbg(ATH6KL_DBG_HTC_SEND, "%s: transmit len : %d (%s)\n",
  153. __func__, send_len, sync ? "sync" : "async");
  154. padded_len = CALC_TXRX_PADDED_LEN(target, send_len);
  155. ath6kl_dbg(ATH6KL_DBG_HTC_SEND,
  156. "DevSendPacket, padded len: %d mbox:0x%X (mode:%s)\n",
  157. padded_len,
  158. target->dev->ar->mbox_info.htc_addr,
  159. sync ? "sync" : "async");
  160. if (sync) {
  161. status = hif_read_write_sync(target->dev->ar,
  162. target->dev->ar->mbox_info.htc_addr,
  163. packet->buf, padded_len,
  164. HIF_WR_SYNC_BLOCK_INC);
  165. packet->status = status;
  166. packet->buf += HTC_HDR_LENGTH;
  167. } else
  168. status = hif_write_async(target->dev->ar,
  169. target->dev->ar->mbox_info.htc_addr,
  170. packet->buf, padded_len,
  171. HIF_WR_ASYNC_BLOCK_INC, packet);
  172. return status;
  173. }
  174. static int htc_check_credits(struct htc_target *target,
  175. struct htc_endpoint *ep, u8 *flags,
  176. enum htc_endpoint_id eid, unsigned int len,
  177. int *req_cred)
  178. {
  179. *req_cred = (len > target->tgt_cred_sz) ?
  180. DIV_ROUND_UP(len, target->tgt_cred_sz) : 1;
  181. ath6kl_dbg(ATH6KL_DBG_HTC_SEND, "creds required:%d got:%d\n",
  182. *req_cred, ep->cred_dist.credits);
  183. if (ep->cred_dist.credits < *req_cred) {
  184. if (eid == ENDPOINT_0)
  185. return -EINVAL;
  186. /* Seek more credits */
  187. ep->cred_dist.seek_cred = *req_cred - ep->cred_dist.credits;
  188. ath6kl_dbg(ATH6KL_DBG_HTC_SEND, "ctxt:0x%p dist:0x%p\n",
  189. target->cred_dist_cntxt, &ep->cred_dist);
  190. ath6k_seek_credits(target->cred_dist_cntxt, &ep->cred_dist);
  191. ep->cred_dist.seek_cred = 0;
  192. if (ep->cred_dist.credits < *req_cred) {
  193. ath6kl_dbg(ATH6KL_DBG_HTC_SEND,
  194. "not enough credits for ep %d - leaving packet in queue\n",
  195. eid);
  196. return -EINVAL;
  197. }
  198. }
  199. ep->cred_dist.credits -= *req_cred;
  200. ep->ep_st.cred_cosumd += *req_cred;
  201. /* When we are getting low on credits, ask for more */
  202. if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
  203. ep->cred_dist.seek_cred =
  204. ep->cred_dist.cred_per_msg - ep->cred_dist.credits;
  205. ath6kl_dbg(ATH6KL_DBG_HTC_SEND, "ctxt:0x%p dist:0x%p\n",
  206. target->cred_dist_cntxt, &ep->cred_dist);
  207. ath6k_seek_credits(target->cred_dist_cntxt, &ep->cred_dist);
  208. /* see if we were successful in getting more */
  209. if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
  210. /* tell the target we need credits ASAP! */
  211. *flags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  212. ep->ep_st.cred_low_indicate += 1;
  213. ath6kl_dbg(ATH6KL_DBG_HTC_SEND, "host needs credits\n");
  214. }
  215. }
  216. return 0;
  217. }
  218. static void htc_tx_pkts_get(struct htc_target *target,
  219. struct htc_endpoint *endpoint,
  220. struct list_head *queue)
  221. {
  222. int req_cred;
  223. u8 flags;
  224. struct htc_packet *packet;
  225. unsigned int len;
  226. while (true) {
  227. flags = 0;
  228. if (list_empty(&endpoint->txq))
  229. break;
  230. packet = list_first_entry(&endpoint->txq, struct htc_packet,
  231. list);
  232. ath6kl_dbg(ATH6KL_DBG_HTC_SEND,
  233. "got head pkt:0x%p , queue depth: %d\n",
  234. packet, get_queue_depth(&endpoint->txq));
  235. len = CALC_TXRX_PADDED_LEN(target,
  236. packet->act_len + HTC_HDR_LENGTH);
  237. if (htc_check_credits(target, endpoint, &flags,
  238. packet->endpoint, len, &req_cred))
  239. break;
  240. /* now we can fully move onto caller's queue */
  241. packet = list_first_entry(&endpoint->txq, struct htc_packet,
  242. list);
  243. list_move_tail(&packet->list, queue);
  244. /* save the number of credits this packet consumed */
  245. packet->info.tx.cred_used = req_cred;
  246. /* all TX packets are handled asynchronously */
  247. packet->completion = htc_tx_comp_handler;
  248. packet->context = target;
  249. endpoint->ep_st.tx_issued += 1;
  250. /* save send flags */
  251. packet->info.tx.flags = flags;
  252. packet->info.tx.seqno = endpoint->seqno;
  253. endpoint->seqno++;
  254. }
  255. }
  256. /* See if the padded tx length falls on a credit boundary */
  257. static int htc_get_credit_padding(unsigned int cred_sz, int *len,
  258. struct htc_endpoint *ep)
  259. {
  260. int rem_cred, cred_pad;
  261. rem_cred = *len % cred_sz;
  262. /* No padding needed */
  263. if (!rem_cred)
  264. return 0;
  265. if (!(ep->conn_flags & HTC_FLGS_TX_BNDL_PAD_EN))
  266. return -1;
  267. /*
  268. * The transfer consumes a "partial" credit, this
  269. * packet cannot be bundled unless we add
  270. * additional "dummy" padding (max 255 bytes) to
  271. * consume the entire credit.
  272. */
  273. cred_pad = *len < cred_sz ? (cred_sz - *len) : rem_cred;
  274. if ((cred_pad > 0) && (cred_pad <= 255))
  275. *len += cred_pad;
  276. else
  277. /* The amount of padding is too large, send as non-bundled */
  278. return -1;
  279. return cred_pad;
  280. }
  281. static int htc_setup_send_scat_list(struct htc_target *target,
  282. struct htc_endpoint *endpoint,
  283. struct hif_scatter_req *scat_req,
  284. int n_scat,
  285. struct list_head *queue)
  286. {
  287. struct htc_packet *packet;
  288. int i, len, rem_scat, cred_pad;
  289. int status = 0;
  290. rem_scat = target->max_tx_bndl_sz;
  291. for (i = 0; i < n_scat; i++) {
  292. scat_req->scat_list[i].packet = NULL;
  293. if (list_empty(queue))
  294. break;
  295. packet = list_first_entry(queue, struct htc_packet, list);
  296. len = CALC_TXRX_PADDED_LEN(target,
  297. packet->act_len + HTC_HDR_LENGTH);
  298. cred_pad = htc_get_credit_padding(target->tgt_cred_sz,
  299. &len, endpoint);
  300. if (cred_pad < 0 || rem_scat < len) {
  301. status = -ENOSPC;
  302. break;
  303. }
  304. rem_scat -= len;
  305. /* now remove it from the queue */
  306. packet = list_first_entry(queue, struct htc_packet, list);
  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. htc_prep_send_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_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. * htc_issue_send_bundle: drain a queue and send as bundles
  339. * this function may return without fully draining the queue
  340. * when
  341. *
  342. * 1. scatter resources are exhausted
  343. * 2. a message that will consume a partial credit will stop the
  344. * bundling process early
  345. * 3. we drop below the minimum number of messages for a bundle
  346. */
  347. static void htc_issue_send_bundle(struct htc_endpoint *endpoint,
  348. struct list_head *queue,
  349. int *sent_bundle, int *n_bundle_pkts)
  350. {
  351. struct htc_target *target = endpoint->target;
  352. struct hif_scatter_req *scat_req = NULL;
  353. int n_scat, n_sent_bundle = 0, tot_pkts_bundle = 0;
  354. int status;
  355. while (true) {
  356. status = 0;
  357. n_scat = get_queue_depth(queue);
  358. n_scat = min(n_scat, target->msg_per_bndl_max);
  359. if (n_scat < HTC_MIN_HTC_MSGS_TO_BUNDLE)
  360. /* not enough to bundle */
  361. break;
  362. scat_req = hif_scatter_req_get(target->dev->ar);
  363. if (!scat_req) {
  364. /* no scatter resources */
  365. ath6kl_dbg(ATH6KL_DBG_HTC_SEND,
  366. "no more scatter resources\n");
  367. break;
  368. }
  369. ath6kl_dbg(ATH6KL_DBG_HTC_SEND, "pkts to scatter: %d\n",
  370. n_scat);
  371. scat_req->len = 0;
  372. scat_req->scat_entries = 0;
  373. status = htc_setup_send_scat_list(target, endpoint,
  374. scat_req, n_scat, 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. ath6kldev_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, "htc_issue_send_bundle (sent:%d)\n",
  393. n_sent_bundle);
  394. return;
  395. }
  396. static void htc_tx_from_ep_txq(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. 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. htc_issue_send_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. htc_prep_send_pkt(packet, packet->info.tx.flags,
  442. 0, packet->info.tx.seqno);
  443. htc_issue_send(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 htc_try_send(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. htc_tx_from_ep_txq(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. htc_tx_from_ep_txq(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. htc_prep_send_pkt(send_pkt, 0, 0, 0);
  556. status = htc_issue_send(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 (!htc_try_send(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 htc_update_rx_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 dev_rx_pkt(struct htc_target *target, struct htc_packet *packet,
  729. u32 rx_len)
  730. {
  731. struct ath6kl_device *dev = target->dev;
  732. u32 padded_len;
  733. int status;
  734. padded_len = CALC_TXRX_PADDED_LEN(target, rx_len);
  735. if (padded_len > packet->buf_len) {
  736. ath6kl_err("not enough receive space for packet - padlen:%d recvlen:%d bufferlen:%d\n",
  737. padded_len, rx_len, packet->buf_len);
  738. return -ENOMEM;
  739. }
  740. ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
  741. "dev_rx_pkt (0x%p : hdr:0x%X) padded len: %d mbox:0x%X (mode:%s)\n",
  742. packet, packet->info.rx.exp_hdr,
  743. padded_len, dev->ar->mbox_info.htc_addr, "sync");
  744. status = hif_read_write_sync(dev->ar,
  745. dev->ar->mbox_info.htc_addr,
  746. packet->buf, padded_len,
  747. HIF_RD_SYNC_BLOCK_FIX);
  748. packet->status = status;
  749. return status;
  750. }
  751. /*
  752. * optimization for recv packets, we can indicate a
  753. * "hint" that there are more single-packets to fetch
  754. * on this endpoint.
  755. */
  756. static void set_rxpkt_indication_flag(u32 lk_ahd,
  757. struct htc_endpoint *endpoint,
  758. struct htc_packet *packet)
  759. {
  760. struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)&lk_ahd;
  761. if (htc_hdr->eid == packet->endpoint) {
  762. if (!list_empty(&endpoint->rx_bufq))
  763. packet->info.rx.indicat_flags |=
  764. HTC_RX_FLAGS_INDICATE_MORE_PKTS;
  765. }
  766. }
  767. static void chk_rx_water_mark(struct htc_endpoint *endpoint)
  768. {
  769. struct htc_ep_callbacks ep_cb = endpoint->ep_cb;
  770. if (ep_cb.rx_refill_thresh > 0) {
  771. spin_lock_bh(&endpoint->target->rx_lock);
  772. if (get_queue_depth(&endpoint->rx_bufq)
  773. < ep_cb.rx_refill_thresh) {
  774. spin_unlock_bh(&endpoint->target->rx_lock);
  775. ep_cb.rx_refill(endpoint->target, endpoint->eid);
  776. return;
  777. }
  778. spin_unlock_bh(&endpoint->target->rx_lock);
  779. }
  780. }
  781. /* This function is called with rx_lock held */
  782. static int htc_setup_rxpkts(struct htc_target *target, struct htc_endpoint *ep,
  783. u32 *lk_ahds, struct list_head *queue, int n_msg)
  784. {
  785. struct htc_packet *packet;
  786. /* FIXME: type of lk_ahds can't be right */
  787. struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)lk_ahds;
  788. struct htc_ep_callbacks ep_cb;
  789. int status = 0, j, full_len;
  790. bool no_recycle;
  791. full_len = CALC_TXRX_PADDED_LEN(target,
  792. le16_to_cpu(htc_hdr->payld_len) +
  793. sizeof(*htc_hdr));
  794. if (!htc_valid_rx_frame_len(target, ep->eid, full_len)) {
  795. ath6kl_warn("Rx buffer requested with invalid length\n");
  796. return -EINVAL;
  797. }
  798. ep_cb = ep->ep_cb;
  799. for (j = 0; j < n_msg; j++) {
  800. /*
  801. * Reset flag, any packets allocated using the
  802. * rx_alloc() API cannot be recycled on
  803. * cleanup,they must be explicitly returned.
  804. */
  805. no_recycle = false;
  806. if (ep_cb.rx_allocthresh &&
  807. (full_len > ep_cb.rx_alloc_thresh)) {
  808. ep->ep_st.rx_alloc_thresh_hit += 1;
  809. ep->ep_st.rxalloc_thresh_byte +=
  810. le16_to_cpu(htc_hdr->payld_len);
  811. spin_unlock_bh(&target->rx_lock);
  812. no_recycle = true;
  813. packet = ep_cb.rx_allocthresh(ep->target, ep->eid,
  814. full_len);
  815. spin_lock_bh(&target->rx_lock);
  816. } else {
  817. /* refill handler is being used */
  818. if (list_empty(&ep->rx_bufq)) {
  819. if (ep_cb.rx_refill) {
  820. spin_unlock_bh(&target->rx_lock);
  821. ep_cb.rx_refill(ep->target, ep->eid);
  822. spin_lock_bh(&target->rx_lock);
  823. }
  824. }
  825. if (list_empty(&ep->rx_bufq))
  826. packet = NULL;
  827. else {
  828. packet = list_first_entry(&ep->rx_bufq,
  829. struct htc_packet, list);
  830. list_del(&packet->list);
  831. }
  832. }
  833. if (!packet) {
  834. target->rx_st_flags |= HTC_RECV_WAIT_BUFFERS;
  835. target->ep_waiting = ep->eid;
  836. return -ENOSPC;
  837. }
  838. /* clear flags */
  839. packet->info.rx.rx_flags = 0;
  840. packet->info.rx.indicat_flags = 0;
  841. packet->status = 0;
  842. if (no_recycle)
  843. /*
  844. * flag that these packets cannot be
  845. * recycled, they have to be returned to
  846. * the user
  847. */
  848. packet->info.rx.rx_flags |= HTC_RX_PKT_NO_RECYCLE;
  849. /* Caller needs to free this upon any failure */
  850. list_add_tail(&packet->list, queue);
  851. if (target->htc_flags & HTC_OP_STATE_STOPPING) {
  852. status = -ECANCELED;
  853. break;
  854. }
  855. if (j) {
  856. packet->info.rx.rx_flags |= HTC_RX_PKT_REFRESH_HDR;
  857. packet->info.rx.exp_hdr = 0xFFFFFFFF;
  858. } else
  859. /* set expected look ahead */
  860. packet->info.rx.exp_hdr = *lk_ahds;
  861. packet->act_len = le16_to_cpu(htc_hdr->payld_len) +
  862. HTC_HDR_LENGTH;
  863. }
  864. return status;
  865. }
  866. static int alloc_and_prep_rxpkts(struct htc_target *target,
  867. u32 lk_ahds[], int msg,
  868. struct htc_endpoint *endpoint,
  869. struct list_head *queue)
  870. {
  871. int status = 0;
  872. struct htc_packet *packet, *tmp_pkt;
  873. struct htc_frame_hdr *htc_hdr;
  874. int i, n_msg;
  875. spin_lock_bh(&target->rx_lock);
  876. for (i = 0; i < msg; i++) {
  877. htc_hdr = (struct htc_frame_hdr *)&lk_ahds[i];
  878. if (htc_hdr->eid >= ENDPOINT_MAX) {
  879. ath6kl_err("invalid ep in look-ahead: %d\n",
  880. htc_hdr->eid);
  881. status = -ENOMEM;
  882. break;
  883. }
  884. if (htc_hdr->eid != endpoint->eid) {
  885. ath6kl_err("invalid ep in look-ahead: %d should be : %d (index:%d)\n",
  886. htc_hdr->eid, endpoint->eid, i);
  887. status = -ENOMEM;
  888. break;
  889. }
  890. if (le16_to_cpu(htc_hdr->payld_len) > HTC_MAX_PAYLOAD_LENGTH) {
  891. ath6kl_err("payload len %d exceeds max htc : %d !\n",
  892. htc_hdr->payld_len,
  893. (u32) HTC_MAX_PAYLOAD_LENGTH);
  894. status = -ENOMEM;
  895. break;
  896. }
  897. if (endpoint->svc_id == 0) {
  898. ath6kl_err("ep %d is not connected !\n", htc_hdr->eid);
  899. status = -ENOMEM;
  900. break;
  901. }
  902. if (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) {
  903. /*
  904. * HTC header indicates that every packet to follow
  905. * has the same padded length so that it can be
  906. * optimally fetched as a full bundle.
  907. */
  908. n_msg = (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) >>
  909. HTC_FLG_RX_BNDL_CNT_S;
  910. /* the count doesn't include the starter frame */
  911. n_msg++;
  912. if (n_msg > target->msg_per_bndl_max) {
  913. status = -ENOMEM;
  914. break;
  915. }
  916. endpoint->ep_st.rx_bundle_from_hdr += 1;
  917. ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
  918. "htc hdr indicates :%d msg can be fetched as a bundle\n",
  919. n_msg);
  920. } else
  921. /* HTC header only indicates 1 message to fetch */
  922. n_msg = 1;
  923. /* Setup packet buffers for each message */
  924. status = htc_setup_rxpkts(target, endpoint, &lk_ahds[i], queue,
  925. n_msg);
  926. /*
  927. * This is due to unavailabilty of buffers to rx entire data.
  928. * Return no error so that free buffers from queue can be used
  929. * to receive partial data.
  930. */
  931. if (status == -ENOSPC) {
  932. spin_unlock_bh(&target->rx_lock);
  933. return 0;
  934. }
  935. if (status)
  936. break;
  937. }
  938. spin_unlock_bh(&target->rx_lock);
  939. if (status) {
  940. list_for_each_entry_safe(packet, tmp_pkt, queue, list) {
  941. list_del(&packet->list);
  942. htc_reclaim_rxbuf(target, packet,
  943. &target->endpoint[packet->endpoint]);
  944. }
  945. }
  946. return status;
  947. }
  948. static void htc_ctrl_rx(struct htc_target *context, struct htc_packet *packets)
  949. {
  950. if (packets->endpoint != ENDPOINT_0) {
  951. WARN_ON(1);
  952. return;
  953. }
  954. if (packets->status == -ECANCELED) {
  955. reclaim_rx_ctrl_buf(context, packets);
  956. return;
  957. }
  958. if (packets->act_len > 0) {
  959. ath6kl_err("htc_ctrl_rx, got message with len:%zu\n",
  960. packets->act_len + HTC_HDR_LENGTH);
  961. ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES,
  962. "Unexpected ENDPOINT 0 Message",
  963. packets->buf - HTC_HDR_LENGTH,
  964. packets->act_len + HTC_HDR_LENGTH);
  965. }
  966. htc_reclaim_rxbuf(context, packets, &context->endpoint[0]);
  967. }
  968. static void htc_proc_cred_rpt(struct htc_target *target,
  969. struct htc_credit_report *rpt,
  970. int n_entries,
  971. enum htc_endpoint_id from_ep)
  972. {
  973. struct htc_endpoint *endpoint;
  974. int tot_credits = 0, i;
  975. bool dist = false;
  976. ath6kl_dbg(ATH6KL_DBG_HTC_SEND,
  977. "htc_proc_cred_rpt, credit report entries:%d\n", n_entries);
  978. spin_lock_bh(&target->tx_lock);
  979. for (i = 0; i < n_entries; i++, rpt++) {
  980. if (rpt->eid >= ENDPOINT_MAX) {
  981. WARN_ON(1);
  982. spin_unlock_bh(&target->tx_lock);
  983. return;
  984. }
  985. endpoint = &target->endpoint[rpt->eid];
  986. ath6kl_dbg(ATH6KL_DBG_HTC_SEND, " ep %d got %d credits\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_SEND,
  1022. "report indicated %d credits to distribute\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_SEND, "ctxt:0x%p dist:0x%p\n",
  1030. target->cred_dist_cntxt, &target->cred_dist_list);
  1031. ath6k_credit_distribute(target->cred_dist_cntxt,
  1032. &target->cred_dist_list,
  1033. HTC_CREDIT_DIST_SEND_COMPLETE);
  1034. }
  1035. spin_unlock_bh(&target->tx_lock);
  1036. if (tot_credits)
  1037. htc_chk_ep_txq(target);
  1038. }
  1039. static int htc_parse_trailer(struct htc_target *target,
  1040. struct htc_record_hdr *record,
  1041. u8 *record_buf, u32 *next_lk_ahds,
  1042. enum htc_endpoint_id endpoint,
  1043. int *n_lk_ahds)
  1044. {
  1045. struct htc_bundle_lkahd_rpt *bundle_lkahd_rpt;
  1046. struct htc_lookahead_report *lk_ahd;
  1047. int len;
  1048. switch (record->rec_id) {
  1049. case HTC_RECORD_CREDITS:
  1050. len = record->len / sizeof(struct htc_credit_report);
  1051. if (!len) {
  1052. WARN_ON(1);
  1053. return -EINVAL;
  1054. }
  1055. htc_proc_cred_rpt(target,
  1056. (struct htc_credit_report *) record_buf,
  1057. len, endpoint);
  1058. break;
  1059. case HTC_RECORD_LOOKAHEAD:
  1060. len = record->len / sizeof(*lk_ahd);
  1061. if (!len) {
  1062. WARN_ON(1);
  1063. return -EINVAL;
  1064. }
  1065. lk_ahd = (struct htc_lookahead_report *) record_buf;
  1066. if ((lk_ahd->pre_valid == ((~lk_ahd->post_valid) & 0xFF))
  1067. && next_lk_ahds) {
  1068. ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
  1069. "lk_ahd report 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_RAW_BYTES, "Next Look Ahead",
  1074. next_lk_ahds, 4);
  1075. *n_lk_ahds = 1;
  1076. }
  1077. break;
  1078. case HTC_RECORD_LOOKAHEAD_BUNDLE:
  1079. len = record->len / sizeof(*bundle_lkahd_rpt);
  1080. if (!len || (len > HTC_HOST_MAX_MSG_PER_BUNDLE)) {
  1081. WARN_ON(1);
  1082. return -EINVAL;
  1083. }
  1084. if (next_lk_ahds) {
  1085. int i;
  1086. bundle_lkahd_rpt =
  1087. (struct htc_bundle_lkahd_rpt *) record_buf;
  1088. ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, "Bundle lk_ahd",
  1089. record_buf, record->len);
  1090. for (i = 0; i < len; i++) {
  1091. memcpy((u8 *)&next_lk_ahds[i],
  1092. bundle_lkahd_rpt->lk_ahd, 4);
  1093. bundle_lkahd_rpt++;
  1094. }
  1095. *n_lk_ahds = i;
  1096. }
  1097. break;
  1098. default:
  1099. ath6kl_err("unhandled record: id:%d len:%d\n",
  1100. record->rec_id, record->len);
  1101. break;
  1102. }
  1103. return 0;
  1104. }
  1105. static int htc_proc_trailer(struct htc_target *target,
  1106. u8 *buf, int len, u32 *next_lk_ahds,
  1107. int *n_lk_ahds, enum htc_endpoint_id endpoint)
  1108. {
  1109. struct htc_record_hdr *record;
  1110. int orig_len;
  1111. int status;
  1112. u8 *record_buf;
  1113. u8 *orig_buf;
  1114. ath6kl_dbg(ATH6KL_DBG_HTC_RECV, "+htc_proc_trailer (len:%d)\n", len);
  1115. ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, "Recv Trailer", buf, len);
  1116. orig_buf = buf;
  1117. orig_len = len;
  1118. status = 0;
  1119. while (len > 0) {
  1120. if (len < sizeof(struct htc_record_hdr)) {
  1121. status = -ENOMEM;
  1122. break;
  1123. }
  1124. /* these are byte aligned structs */
  1125. record = (struct htc_record_hdr *) buf;
  1126. len -= sizeof(struct htc_record_hdr);
  1127. buf += sizeof(struct htc_record_hdr);
  1128. if (record->len > len) {
  1129. ath6kl_err("invalid record len: %d (id:%d) buf has: %d bytes left\n",
  1130. record->len, record->rec_id, len);
  1131. status = -ENOMEM;
  1132. break;
  1133. }
  1134. record_buf = buf;
  1135. status = htc_parse_trailer(target, record, record_buf,
  1136. next_lk_ahds, endpoint, n_lk_ahds);
  1137. if (status)
  1138. break;
  1139. /* advance buffer past this record for next time around */
  1140. buf += record->len;
  1141. len -= record->len;
  1142. }
  1143. if (status)
  1144. ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, "BAD Recv Trailer",
  1145. orig_buf, orig_len);
  1146. return status;
  1147. }
  1148. static int htc_proc_rxhdr(struct htc_target *target,
  1149. struct htc_packet *packet,
  1150. u32 *next_lkahds, int *n_lkahds)
  1151. {
  1152. int status = 0;
  1153. u16 payload_len;
  1154. u32 lk_ahd;
  1155. struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)packet->buf;
  1156. if (n_lkahds != NULL)
  1157. *n_lkahds = 0;
  1158. ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, "HTC Recv PKT", packet->buf,
  1159. packet->act_len);
  1160. /*
  1161. * NOTE: we cannot assume the alignment of buf, so we use the safe
  1162. * macros to retrieve 16 bit fields.
  1163. */
  1164. payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
  1165. memcpy((u8 *)&lk_ahd, packet->buf, sizeof(lk_ahd));
  1166. if (packet->info.rx.rx_flags & HTC_RX_PKT_REFRESH_HDR) {
  1167. /*
  1168. * Refresh the expected header and the actual length as it
  1169. * was unknown when this packet was grabbed as part of the
  1170. * bundle.
  1171. */
  1172. packet->info.rx.exp_hdr = lk_ahd;
  1173. packet->act_len = payload_len + HTC_HDR_LENGTH;
  1174. /* validate the actual header that was refreshed */
  1175. if (packet->act_len > packet->buf_len) {
  1176. ath6kl_err("refreshed hdr payload len (%d) in bundled recv is invalid (hdr: 0x%X)\n",
  1177. payload_len, lk_ahd);
  1178. /*
  1179. * Limit this to max buffer just to print out some
  1180. * of the buffer.
  1181. */
  1182. packet->act_len = min(packet->act_len, packet->buf_len);
  1183. status = -ENOMEM;
  1184. goto fail_rx;
  1185. }
  1186. if (packet->endpoint != htc_hdr->eid) {
  1187. ath6kl_err("refreshed hdr ep (%d) does not match expected ep (%d)\n",
  1188. htc_hdr->eid, packet->endpoint);
  1189. status = -ENOMEM;
  1190. goto fail_rx;
  1191. }
  1192. }
  1193. if (lk_ahd != packet->info.rx.exp_hdr) {
  1194. ath6kl_err("htc_proc_rxhdr, lk_ahd mismatch! (pPkt:0x%p flags:0x%X)\n",
  1195. packet, packet->info.rx.rx_flags);
  1196. ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, "Expected Message lk_ahd",
  1197. &packet->info.rx.exp_hdr, 4);
  1198. ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, "Current Frame Header",
  1199. (u8 *)&lk_ahd, sizeof(lk_ahd));
  1200. status = -ENOMEM;
  1201. goto fail_rx;
  1202. }
  1203. if (htc_hdr->flags & HTC_FLG_RX_TRAILER) {
  1204. if (htc_hdr->ctrl[0] < sizeof(struct htc_record_hdr) ||
  1205. htc_hdr->ctrl[0] > payload_len) {
  1206. ath6kl_err("htc_proc_rxhdr, invalid hdr (payload len should be :%d, CB[0] is:%d)\n",
  1207. payload_len, htc_hdr->ctrl[0]);
  1208. status = -ENOMEM;
  1209. goto fail_rx;
  1210. }
  1211. if (packet->info.rx.rx_flags & HTC_RX_PKT_IGNORE_LOOKAHEAD) {
  1212. next_lkahds = NULL;
  1213. n_lkahds = NULL;
  1214. }
  1215. status = htc_proc_trailer(target, packet->buf + HTC_HDR_LENGTH
  1216. + payload_len - htc_hdr->ctrl[0],
  1217. htc_hdr->ctrl[0], next_lkahds,
  1218. n_lkahds, packet->endpoint);
  1219. if (status)
  1220. goto fail_rx;
  1221. packet->act_len -= htc_hdr->ctrl[0];
  1222. }
  1223. packet->buf += HTC_HDR_LENGTH;
  1224. packet->act_len -= HTC_HDR_LENGTH;
  1225. fail_rx:
  1226. if (status)
  1227. ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, "BAD HTC Recv PKT",
  1228. packet->buf,
  1229. packet->act_len < 256 ? packet->act_len : 256);
  1230. else {
  1231. if (packet->act_len > 0)
  1232. ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES,
  1233. "HTC - Application Msg",
  1234. packet->buf, packet->act_len);
  1235. }
  1236. return status;
  1237. }
  1238. static void do_rx_completion(struct htc_endpoint *endpoint,
  1239. struct htc_packet *packet)
  1240. {
  1241. ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
  1242. "htc calling ep %d recv callback on packet 0x%p\n",
  1243. endpoint->eid, packet);
  1244. endpoint->ep_cb.rx(endpoint->target, packet);
  1245. }
  1246. static int htc_issue_rxpkt_bundle(struct htc_target *target,
  1247. struct list_head *rxq,
  1248. struct list_head *sync_compq,
  1249. int *n_pkt_fetched, bool part_bundle)
  1250. {
  1251. struct hif_scatter_req *scat_req;
  1252. struct htc_packet *packet;
  1253. int rem_space = target->max_rx_bndl_sz;
  1254. int n_scat_pkt, status = 0, i, len;
  1255. n_scat_pkt = get_queue_depth(rxq);
  1256. n_scat_pkt = min(n_scat_pkt, target->msg_per_bndl_max);
  1257. if ((get_queue_depth(rxq) - n_scat_pkt) > 0) {
  1258. /*
  1259. * We were forced to split this bundle receive operation
  1260. * all packets in this partial bundle must have their
  1261. * lookaheads ignored.
  1262. */
  1263. part_bundle = true;
  1264. /*
  1265. * This would only happen if the target ignored our max
  1266. * bundle limit.
  1267. */
  1268. ath6kl_warn("htc_issue_rxpkt_bundle : partial bundle detected num:%d , %d\n",
  1269. get_queue_depth(rxq), n_scat_pkt);
  1270. }
  1271. len = 0;
  1272. ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
  1273. "htc_issue_rxpkt_bundle (numpackets: %d , actual : %d)\n",
  1274. get_queue_depth(rxq), n_scat_pkt);
  1275. scat_req = hif_scatter_req_get(target->dev->ar);
  1276. if (scat_req == NULL)
  1277. goto fail_rx_pkt;
  1278. for (i = 0; i < n_scat_pkt; i++) {
  1279. int pad_len;
  1280. packet = list_first_entry(rxq, struct htc_packet, list);
  1281. list_del(&packet->list);
  1282. pad_len = CALC_TXRX_PADDED_LEN(target,
  1283. packet->act_len);
  1284. if ((rem_space - pad_len) < 0) {
  1285. list_add(&packet->list, rxq);
  1286. break;
  1287. }
  1288. rem_space -= pad_len;
  1289. if (part_bundle || (i < (n_scat_pkt - 1)))
  1290. /*
  1291. * Packet 0..n-1 cannot be checked for look-aheads
  1292. * since we are fetching a bundle the last packet
  1293. * however can have it's lookahead used
  1294. */
  1295. packet->info.rx.rx_flags |=
  1296. HTC_RX_PKT_IGNORE_LOOKAHEAD;
  1297. /* NOTE: 1 HTC packet per scatter entry */
  1298. scat_req->scat_list[i].buf = packet->buf;
  1299. scat_req->scat_list[i].len = pad_len;
  1300. packet->info.rx.rx_flags |= HTC_RX_PKT_PART_OF_BUNDLE;
  1301. list_add_tail(&packet->list, sync_compq);
  1302. WARN_ON(!scat_req->scat_list[i].len);
  1303. len += scat_req->scat_list[i].len;
  1304. }
  1305. scat_req->len = len;
  1306. scat_req->scat_entries = i;
  1307. status = ath6kldev_submit_scat_req(target->dev, scat_req, true);
  1308. if (!status)
  1309. *n_pkt_fetched = i;
  1310. /* free scatter request */
  1311. hif_scatter_req_add(target->dev->ar, scat_req);
  1312. fail_rx_pkt:
  1313. return status;
  1314. }
  1315. static int htc_proc_fetched_rxpkts(struct htc_target *target,
  1316. struct list_head *comp_pktq, u32 lk_ahds[],
  1317. int *n_lk_ahd)
  1318. {
  1319. struct htc_packet *packet, *tmp_pkt;
  1320. struct htc_endpoint *ep;
  1321. int status = 0;
  1322. list_for_each_entry_safe(packet, tmp_pkt, comp_pktq, list) {
  1323. list_del(&packet->list);
  1324. ep = &target->endpoint[packet->endpoint];
  1325. /* process header for each of the recv packet */
  1326. status = htc_proc_rxhdr(target, packet, lk_ahds, n_lk_ahd);
  1327. if (status)
  1328. return status;
  1329. if (list_empty(comp_pktq)) {
  1330. /*
  1331. * Last packet's more packet flag is set
  1332. * based on the lookahead.
  1333. */
  1334. if (*n_lk_ahd > 0)
  1335. set_rxpkt_indication_flag(lk_ahds[0],
  1336. ep, packet);
  1337. } else
  1338. /*
  1339. * Packets in a bundle automatically have
  1340. * this flag set.
  1341. */
  1342. packet->info.rx.indicat_flags |=
  1343. HTC_RX_FLAGS_INDICATE_MORE_PKTS;
  1344. htc_update_rx_stats(ep, *n_lk_ahd);
  1345. if (packet->info.rx.rx_flags & HTC_RX_PKT_PART_OF_BUNDLE)
  1346. ep->ep_st.rx_bundl += 1;
  1347. do_rx_completion(ep, packet);
  1348. }
  1349. return status;
  1350. }
  1351. static int htc_fetch_rxpkts(struct htc_target *target,
  1352. struct list_head *rx_pktq,
  1353. struct list_head *comp_pktq)
  1354. {
  1355. int fetched_pkts;
  1356. bool part_bundle = false;
  1357. int status = 0;
  1358. /* now go fetch the list of HTC packets */
  1359. while (!list_empty(rx_pktq)) {
  1360. fetched_pkts = 0;
  1361. if (target->rx_bndl_enable && (get_queue_depth(rx_pktq) > 1)) {
  1362. /*
  1363. * There are enough packets to attempt a
  1364. * bundle transfer and recv bundling is
  1365. * allowed.
  1366. */
  1367. status = htc_issue_rxpkt_bundle(target, rx_pktq,
  1368. comp_pktq,
  1369. &fetched_pkts,
  1370. part_bundle);
  1371. if (status)
  1372. return status;
  1373. if (!list_empty(rx_pktq))
  1374. part_bundle = true;
  1375. }
  1376. if (!fetched_pkts) {
  1377. struct htc_packet *packet;
  1378. packet = list_first_entry(rx_pktq, struct htc_packet,
  1379. list);
  1380. list_del(&packet->list);
  1381. /* fully synchronous */
  1382. packet->completion = NULL;
  1383. if (!list_empty(rx_pktq))
  1384. /*
  1385. * look_aheads in all packet
  1386. * except the last one in the
  1387. * bundle must be ignored
  1388. */
  1389. packet->info.rx.rx_flags |=
  1390. HTC_RX_PKT_IGNORE_LOOKAHEAD;
  1391. /* go fetch the packet */
  1392. status = dev_rx_pkt(target, packet, packet->act_len);
  1393. if (status)
  1394. return status;
  1395. list_add_tail(&packet->list, comp_pktq);
  1396. }
  1397. }
  1398. return status;
  1399. }
  1400. int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
  1401. u32 msg_look_ahead[], int *num_pkts)
  1402. {
  1403. struct htc_packet *packets, *tmp_pkt;
  1404. struct htc_endpoint *endpoint;
  1405. struct list_head rx_pktq, comp_pktq;
  1406. int status = 0;
  1407. u32 look_aheads[HTC_HOST_MAX_MSG_PER_BUNDLE];
  1408. int num_look_ahead = 1;
  1409. enum htc_endpoint_id id;
  1410. int n_fetched = 0;
  1411. *num_pkts = 0;
  1412. /*
  1413. * On first entry copy the look_aheads into our temp array for
  1414. * processing
  1415. */
  1416. memcpy(look_aheads, msg_look_ahead, sizeof(look_aheads));
  1417. while (true) {
  1418. /*
  1419. * First lookahead sets the expected endpoint IDs for all
  1420. * packets in a bundle.
  1421. */
  1422. id = ((struct htc_frame_hdr *)&look_aheads[0])->eid;
  1423. endpoint = &target->endpoint[id];
  1424. if (id >= ENDPOINT_MAX) {
  1425. ath6kl_err("MsgPend, invalid endpoint in look-ahead: %d\n",
  1426. id);
  1427. status = -ENOMEM;
  1428. break;
  1429. }
  1430. INIT_LIST_HEAD(&rx_pktq);
  1431. INIT_LIST_HEAD(&comp_pktq);
  1432. /*
  1433. * Try to allocate as many HTC RX packets indicated by the
  1434. * look_aheads.
  1435. */
  1436. status = alloc_and_prep_rxpkts(target, look_aheads,
  1437. num_look_ahead, endpoint,
  1438. &rx_pktq);
  1439. if (status)
  1440. break;
  1441. if (get_queue_depth(&rx_pktq) >= 2)
  1442. /*
  1443. * A recv bundle was detected, force IRQ status
  1444. * re-check again
  1445. */
  1446. target->chk_irq_status_cnt = 1;
  1447. n_fetched += get_queue_depth(&rx_pktq);
  1448. num_look_ahead = 0;
  1449. status = htc_fetch_rxpkts(target, &rx_pktq, &comp_pktq);
  1450. if (!status)
  1451. chk_rx_water_mark(endpoint);
  1452. /* Process fetched packets */
  1453. status = htc_proc_fetched_rxpkts(target, &comp_pktq,
  1454. look_aheads, &num_look_ahead);
  1455. if (!num_look_ahead || status)
  1456. break;
  1457. /*
  1458. * For SYNCH processing, if we get here, we are running
  1459. * through the loop again due to a detected lookahead. Set
  1460. * flag that we should re-check IRQ status registers again
  1461. * before leaving IRQ processing, this can net better
  1462. * performance in high throughput situations.
  1463. */
  1464. target->chk_irq_status_cnt = 1;
  1465. }
  1466. if (status) {
  1467. ath6kl_err("failed to get pending recv messages: %d\n",
  1468. status);
  1469. /*
  1470. * Cleanup any packets we allocated but didn't use to
  1471. * actually fetch any packets.
  1472. */
  1473. list_for_each_entry_safe(packets, tmp_pkt, &rx_pktq, list) {
  1474. list_del(&packets->list);
  1475. htc_reclaim_rxbuf(target, packets,
  1476. &target->endpoint[packets->endpoint]);
  1477. }
  1478. /* cleanup any packets in sync completion queue */
  1479. list_for_each_entry_safe(packets, tmp_pkt, &comp_pktq, list) {
  1480. list_del(&packets->list);
  1481. htc_reclaim_rxbuf(target, packets,
  1482. &target->endpoint[packets->endpoint]);
  1483. }
  1484. if (target->htc_flags & HTC_OP_STATE_STOPPING) {
  1485. ath6kl_warn("host is going to stop blocking receiver for htc_stop\n");
  1486. ath6kldev_rx_control(target->dev, false);
  1487. }
  1488. }
  1489. /*
  1490. * Before leaving, check to see if host ran out of buffers and
  1491. * needs to stop the receiver.
  1492. */
  1493. if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
  1494. ath6kl_warn("host has no rx buffers blocking receiver to prevent overrun\n");
  1495. ath6kldev_rx_control(target->dev, false);
  1496. }
  1497. *num_pkts = n_fetched;
  1498. return status;
  1499. }
  1500. /*
  1501. * Synchronously wait for a control message from the target,
  1502. * This function is used at initialization time ONLY. At init messages
  1503. * on ENDPOINT 0 are expected.
  1504. */
  1505. static struct htc_packet *htc_wait_for_ctrl_msg(struct htc_target *target)
  1506. {
  1507. struct htc_packet *packet = NULL;
  1508. struct htc_frame_hdr *htc_hdr;
  1509. u32 look_ahead;
  1510. if (ath6kldev_poll_mboxmsg_rx(target->dev, &look_ahead,
  1511. HTC_TARGET_RESPONSE_TIMEOUT))
  1512. return NULL;
  1513. ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
  1514. "htc_wait_for_ctrl_msg: look_ahead : 0x%X\n", look_ahead);
  1515. htc_hdr = (struct htc_frame_hdr *)&look_ahead;
  1516. if (htc_hdr->eid != ENDPOINT_0)
  1517. return NULL;
  1518. packet = htc_get_control_buf(target, false);
  1519. if (!packet)
  1520. return NULL;
  1521. packet->info.rx.rx_flags = 0;
  1522. packet->info.rx.exp_hdr = look_ahead;
  1523. packet->act_len = le16_to_cpu(htc_hdr->payld_len) + HTC_HDR_LENGTH;
  1524. if (packet->act_len > packet->buf_len)
  1525. goto fail_ctrl_rx;
  1526. /* we want synchronous operation */
  1527. packet->completion = NULL;
  1528. /* get the message from the device, this will block */
  1529. if (dev_rx_pkt(target, packet, packet->act_len))
  1530. goto fail_ctrl_rx;
  1531. /* process receive header */
  1532. packet->status = htc_proc_rxhdr(target, packet, NULL, NULL);
  1533. if (packet->status) {
  1534. ath6kl_err("htc_wait_for_ctrl_msg, htc_proc_rxhdr failed (status = %d)\n",
  1535. packet->status);
  1536. goto fail_ctrl_rx;
  1537. }
  1538. return packet;
  1539. fail_ctrl_rx:
  1540. if (packet != NULL) {
  1541. htc_rxpkt_reset(packet);
  1542. reclaim_rx_ctrl_buf(target, packet);
  1543. }
  1544. return NULL;
  1545. }
  1546. int ath6kl_htc_add_rxbuf_multiple(struct htc_target *target,
  1547. struct list_head *pkt_queue)
  1548. {
  1549. struct htc_endpoint *endpoint;
  1550. struct htc_packet *first_pkt;
  1551. bool rx_unblock = false;
  1552. int status = 0, depth;
  1553. if (list_empty(pkt_queue))
  1554. return -ENOMEM;
  1555. first_pkt = list_first_entry(pkt_queue, struct htc_packet, list);
  1556. if (first_pkt->endpoint >= ENDPOINT_MAX)
  1557. return status;
  1558. depth = get_queue_depth(pkt_queue);
  1559. ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
  1560. "htc_add_rxbuf_multiple: ep id: %d, cnt:%d, len: %d\n",
  1561. first_pkt->endpoint, depth, first_pkt->buf_len);
  1562. endpoint = &target->endpoint[first_pkt->endpoint];
  1563. if (target->htc_flags & HTC_OP_STATE_STOPPING) {
  1564. struct htc_packet *packet, *tmp_pkt;
  1565. /* walk through queue and mark each one canceled */
  1566. list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
  1567. packet->status = -ECANCELED;
  1568. list_del(&packet->list);
  1569. do_rx_completion(endpoint, packet);
  1570. }
  1571. return status;
  1572. }
  1573. spin_lock_bh(&target->rx_lock);
  1574. list_splice_tail_init(pkt_queue, &endpoint->rx_bufq);
  1575. /* check if we are blocked waiting for a new buffer */
  1576. if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
  1577. if (target->ep_waiting == first_pkt->endpoint) {
  1578. ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
  1579. "receiver was blocked on ep:%d, unblocking.\n",
  1580. target->ep_waiting);
  1581. target->rx_st_flags &= ~HTC_RECV_WAIT_BUFFERS;
  1582. target->ep_waiting = ENDPOINT_MAX;
  1583. rx_unblock = true;
  1584. }
  1585. }
  1586. spin_unlock_bh(&target->rx_lock);
  1587. if (rx_unblock && !(target->htc_flags & HTC_OP_STATE_STOPPING))
  1588. /* TODO : implement a buffer threshold count? */
  1589. ath6kldev_rx_control(target->dev, true);
  1590. return status;
  1591. }
  1592. void ath6kl_htc_flush_rx_buf(struct htc_target *target)
  1593. {
  1594. struct htc_endpoint *endpoint;
  1595. struct htc_packet *packet, *tmp_pkt;
  1596. int i;
  1597. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  1598. endpoint = &target->endpoint[i];
  1599. if (!endpoint->svc_id)
  1600. /* not in use.. */
  1601. continue;
  1602. spin_lock_bh(&target->rx_lock);
  1603. list_for_each_entry_safe(packet, tmp_pkt,
  1604. &endpoint->rx_bufq, list) {
  1605. list_del(&packet->list);
  1606. spin_unlock_bh(&target->rx_lock);
  1607. ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
  1608. "flushing rx pkt:0x%p, len:%d, ep:%d\n",
  1609. packet, packet->buf_len,
  1610. packet->endpoint);
  1611. dev_kfree_skb(packet->pkt_cntxt);
  1612. spin_lock_bh(&target->rx_lock);
  1613. }
  1614. spin_unlock_bh(&target->rx_lock);
  1615. }
  1616. }
  1617. int ath6kl_htc_conn_service(struct htc_target *target,
  1618. struct htc_service_connect_req *conn_req,
  1619. struct htc_service_connect_resp *conn_resp)
  1620. {
  1621. struct htc_packet *rx_pkt = NULL;
  1622. struct htc_packet *tx_pkt = NULL;
  1623. struct htc_conn_service_resp *resp_msg;
  1624. struct htc_conn_service_msg *conn_msg;
  1625. struct htc_endpoint *endpoint;
  1626. enum htc_endpoint_id assigned_ep = ENDPOINT_MAX;
  1627. unsigned int max_msg_sz = 0;
  1628. int status = 0;
  1629. ath6kl_dbg(ATH6KL_DBG_TRC,
  1630. "htc_conn_service, target:0x%p service id:0x%X\n",
  1631. target, conn_req->svc_id);
  1632. if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
  1633. /* special case for pseudo control service */
  1634. assigned_ep = ENDPOINT_0;
  1635. max_msg_sz = HTC_MAX_CTRL_MSG_LEN;
  1636. } else {
  1637. /* allocate a packet to send to the target */
  1638. tx_pkt = htc_get_control_buf(target, true);
  1639. if (!tx_pkt)
  1640. return -ENOMEM;
  1641. conn_msg = (struct htc_conn_service_msg *)tx_pkt->buf;
  1642. memset(conn_msg, 0, sizeof(*conn_msg));
  1643. conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
  1644. conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
  1645. conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags);
  1646. set_htc_pkt_info(tx_pkt, NULL, (u8 *) conn_msg,
  1647. sizeof(*conn_msg) + conn_msg->svc_meta_len,
  1648. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  1649. /* we want synchronous operation */
  1650. tx_pkt->completion = NULL;
  1651. htc_prep_send_pkt(tx_pkt, 0, 0, 0);
  1652. status = htc_issue_send(target, tx_pkt);
  1653. if (status)
  1654. goto fail_tx;
  1655. /* wait for response */
  1656. rx_pkt = htc_wait_for_ctrl_msg(target);
  1657. if (!rx_pkt) {
  1658. status = -ENOMEM;
  1659. goto fail_tx;
  1660. }
  1661. resp_msg = (struct htc_conn_service_resp *)rx_pkt->buf;
  1662. if ((le16_to_cpu(resp_msg->msg_id) != HTC_MSG_CONN_SVC_RESP_ID)
  1663. || (rx_pkt->act_len < sizeof(*resp_msg))) {
  1664. status = -ENOMEM;
  1665. goto fail_tx;
  1666. }
  1667. conn_resp->resp_code = resp_msg->status;
  1668. /* check response status */
  1669. if (resp_msg->status != HTC_SERVICE_SUCCESS) {
  1670. ath6kl_err("target failed service 0x%X connect request (status:%d)\n",
  1671. resp_msg->svc_id, resp_msg->status);
  1672. status = -ENOMEM;
  1673. goto fail_tx;
  1674. }
  1675. assigned_ep = (enum htc_endpoint_id)resp_msg->eid;
  1676. max_msg_sz = le16_to_cpu(resp_msg->max_msg_sz);
  1677. }
  1678. if (assigned_ep >= ENDPOINT_MAX || !max_msg_sz) {
  1679. status = -ENOMEM;
  1680. goto fail_tx;
  1681. }
  1682. endpoint = &target->endpoint[assigned_ep];
  1683. endpoint->eid = assigned_ep;
  1684. if (endpoint->svc_id) {
  1685. status = -ENOMEM;
  1686. goto fail_tx;
  1687. }
  1688. /* return assigned endpoint to caller */
  1689. conn_resp->endpoint = assigned_ep;
  1690. conn_resp->len_max = max_msg_sz;
  1691. /* setup the endpoint */
  1692. /* this marks the endpoint in use */
  1693. endpoint->svc_id = conn_req->svc_id;
  1694. endpoint->max_txq_depth = conn_req->max_txq_depth;
  1695. endpoint->len_max = max_msg_sz;
  1696. endpoint->ep_cb = conn_req->ep_cb;
  1697. endpoint->cred_dist.svc_id = conn_req->svc_id;
  1698. endpoint->cred_dist.htc_rsvd = endpoint;
  1699. endpoint->cred_dist.endpoint = assigned_ep;
  1700. endpoint->cred_dist.cred_sz = target->tgt_cred_sz;
  1701. if (conn_req->max_rxmsg_sz) {
  1702. /*
  1703. * Override cred_per_msg calculation, this optimizes
  1704. * the credit-low indications since the host will actually
  1705. * issue smaller messages in the Send path.
  1706. */
  1707. if (conn_req->max_rxmsg_sz > max_msg_sz) {
  1708. status = -ENOMEM;
  1709. goto fail_tx;
  1710. }
  1711. endpoint->cred_dist.cred_per_msg =
  1712. conn_req->max_rxmsg_sz / target->tgt_cred_sz;
  1713. } else
  1714. endpoint->cred_dist.cred_per_msg =
  1715. max_msg_sz / target->tgt_cred_sz;
  1716. if (!endpoint->cred_dist.cred_per_msg)
  1717. endpoint->cred_dist.cred_per_msg = 1;
  1718. /* save local connection flags */
  1719. endpoint->conn_flags = conn_req->flags;
  1720. fail_tx:
  1721. if (tx_pkt)
  1722. htc_reclaim_txctrl_buf(target, tx_pkt);
  1723. if (rx_pkt) {
  1724. htc_rxpkt_reset(rx_pkt);
  1725. reclaim_rx_ctrl_buf(target, rx_pkt);
  1726. }
  1727. return status;
  1728. }
  1729. static void reset_ep_state(struct htc_target *target)
  1730. {
  1731. struct htc_endpoint *endpoint;
  1732. int i;
  1733. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  1734. endpoint = &target->endpoint[i];
  1735. memset(&endpoint->cred_dist, 0, sizeof(endpoint->cred_dist));
  1736. endpoint->svc_id = 0;
  1737. endpoint->len_max = 0;
  1738. endpoint->max_txq_depth = 0;
  1739. memset(&endpoint->ep_st, 0,
  1740. sizeof(endpoint->ep_st));
  1741. INIT_LIST_HEAD(&endpoint->rx_bufq);
  1742. INIT_LIST_HEAD(&endpoint->txq);
  1743. endpoint->target = target;
  1744. }
  1745. /* reset distribution list */
  1746. INIT_LIST_HEAD(&target->cred_dist_list);
  1747. }
  1748. int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
  1749. enum htc_endpoint_id endpoint)
  1750. {
  1751. int num;
  1752. spin_lock_bh(&target->rx_lock);
  1753. num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
  1754. spin_unlock_bh(&target->rx_lock);
  1755. return num;
  1756. }
  1757. static void htc_setup_msg_bndl(struct htc_target *target)
  1758. {
  1759. /* limit what HTC can handle */
  1760. target->msg_per_bndl_max = min(HTC_HOST_MAX_MSG_PER_BUNDLE,
  1761. target->msg_per_bndl_max);
  1762. if (ath6kl_hif_enable_scatter(target->dev->ar)) {
  1763. target->msg_per_bndl_max = 0;
  1764. return;
  1765. }
  1766. /* limit bundle what the device layer can handle */
  1767. target->msg_per_bndl_max = min(target->max_scat_entries,
  1768. target->msg_per_bndl_max);
  1769. ath6kl_dbg(ATH6KL_DBG_TRC,
  1770. "htc bundling allowed. max msg per htc bundle: %d\n",
  1771. target->msg_per_bndl_max);
  1772. /* Max rx bundle size is limited by the max tx bundle size */
  1773. target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
  1774. /* Max tx bundle size if limited by the extended mbox address range */
  1775. target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
  1776. target->max_xfer_szper_scatreq);
  1777. ath6kl_dbg(ATH6KL_DBG_ANY, "max recv: %d max send: %d\n",
  1778. target->max_rx_bndl_sz, target->max_tx_bndl_sz);
  1779. if (target->max_tx_bndl_sz)
  1780. target->tx_bndl_enable = true;
  1781. if (target->max_rx_bndl_sz)
  1782. target->rx_bndl_enable = true;
  1783. if ((target->tgt_cred_sz % target->block_sz) != 0) {
  1784. ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
  1785. target->tgt_cred_sz);
  1786. /*
  1787. * Disallow send bundling since the credit size is
  1788. * not aligned to a block size the I/O block
  1789. * padding will spill into the next credit buffer
  1790. * which is fatal.
  1791. */
  1792. target->tx_bndl_enable = false;
  1793. }
  1794. }
  1795. int ath6kl_htc_wait_target(struct htc_target *target)
  1796. {
  1797. struct htc_packet *packet = NULL;
  1798. struct htc_ready_ext_msg *rdy_msg;
  1799. struct htc_service_connect_req connect;
  1800. struct htc_service_connect_resp resp;
  1801. int status;
  1802. /* we should be getting 1 control message that the target is ready */
  1803. packet = htc_wait_for_ctrl_msg(target);
  1804. if (!packet)
  1805. return -ENOMEM;
  1806. /* we controlled the buffer creation so it's properly aligned */
  1807. rdy_msg = (struct htc_ready_ext_msg *)packet->buf;
  1808. if ((le16_to_cpu(rdy_msg->ver2_0_info.msg_id) != HTC_MSG_READY_ID) ||
  1809. (packet->act_len < sizeof(struct htc_ready_msg))) {
  1810. status = -ENOMEM;
  1811. goto fail_wait_target;
  1812. }
  1813. if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
  1814. status = -ENOMEM;
  1815. goto fail_wait_target;
  1816. }
  1817. target->tgt_creds = le16_to_cpu(rdy_msg->ver2_0_info.cred_cnt);
  1818. target->tgt_cred_sz = le16_to_cpu(rdy_msg->ver2_0_info.cred_sz);
  1819. ath6kl_dbg(ATH6KL_DBG_HTC_RECV,
  1820. "target ready: credits: %d credit size: %d\n",
  1821. target->tgt_creds, target->tgt_cred_sz);
  1822. /* check if this is an extended ready message */
  1823. if (packet->act_len >= sizeof(struct htc_ready_ext_msg)) {
  1824. /* this is an extended message */
  1825. target->htc_tgt_ver = rdy_msg->htc_ver;
  1826. target->msg_per_bndl_max = rdy_msg->msg_per_htc_bndl;
  1827. } else {
  1828. /* legacy */
  1829. target->htc_tgt_ver = HTC_VERSION_2P0;
  1830. target->msg_per_bndl_max = 0;
  1831. }
  1832. ath6kl_dbg(ATH6KL_DBG_TRC, "using htc protocol version : %s (%d)\n",
  1833. (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
  1834. target->htc_tgt_ver);
  1835. if (target->msg_per_bndl_max > 0)
  1836. htc_setup_msg_bndl(target);
  1837. /* setup our pseudo HTC control endpoint connection */
  1838. memset(&connect, 0, sizeof(connect));
  1839. memset(&resp, 0, sizeof(resp));
  1840. connect.ep_cb.rx = htc_ctrl_rx;
  1841. connect.ep_cb.rx_refill = NULL;
  1842. connect.ep_cb.tx_full = NULL;
  1843. connect.max_txq_depth = NUM_CONTROL_BUFFERS;
  1844. connect.svc_id = HTC_CTRL_RSVD_SVC;
  1845. /* connect fake service */
  1846. status = ath6kl_htc_conn_service((void *)target, &connect, &resp);
  1847. if (status)
  1848. ath6kl_hif_cleanup_scatter(target->dev->ar);
  1849. fail_wait_target:
  1850. if (packet) {
  1851. htc_rxpkt_reset(packet);
  1852. reclaim_rx_ctrl_buf(target, packet);
  1853. }
  1854. return status;
  1855. }
  1856. /*
  1857. * Start HTC, enable interrupts and let the target know
  1858. * host has finished setup.
  1859. */
  1860. int ath6kl_htc_start(struct htc_target *target)
  1861. {
  1862. struct htc_packet *packet;
  1863. int status;
  1864. /* Disable interrupts at the chip level */
  1865. ath6kldev_disable_intrs(target->dev);
  1866. target->htc_flags = 0;
  1867. target->rx_st_flags = 0;
  1868. /* Push control receive buffers into htc control endpoint */
  1869. while ((packet = htc_get_control_buf(target, false)) != NULL) {
  1870. status = htc_add_rxbuf(target, packet);
  1871. if (status)
  1872. return status;
  1873. }
  1874. /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
  1875. ath6k_credit_init(target->cred_dist_cntxt, &target->cred_dist_list,
  1876. target->tgt_creds);
  1877. dump_cred_dist_stats(target);
  1878. /* Indicate to the target of the setup completion */
  1879. status = htc_setup_tx_complete(target);
  1880. if (status)
  1881. return status;
  1882. /* unmask interrupts */
  1883. status = ath6kldev_unmask_intrs(target->dev);
  1884. if (status)
  1885. ath6kl_htc_stop(target);
  1886. return status;
  1887. }
  1888. /* htc_stop: stop interrupt reception, and flush all queued buffers */
  1889. void ath6kl_htc_stop(struct htc_target *target)
  1890. {
  1891. spin_lock_bh(&target->htc_lock);
  1892. target->htc_flags |= HTC_OP_STATE_STOPPING;
  1893. spin_unlock_bh(&target->htc_lock);
  1894. /*
  1895. * Masking interrupts is a synchronous operation, when this
  1896. * function returns all pending HIF I/O has completed, we can
  1897. * safely flush the queues.
  1898. */
  1899. ath6kldev_mask_intrs(target->dev);
  1900. ath6kl_htc_flush_txep_all(target);
  1901. ath6kl_htc_flush_rx_buf(target);
  1902. reset_ep_state(target);
  1903. }
  1904. void *ath6kl_htc_create(struct ath6kl *ar)
  1905. {
  1906. struct htc_target *target = NULL;
  1907. struct htc_packet *packet;
  1908. int status = 0, i = 0;
  1909. u32 block_size, ctrl_bufsz;
  1910. target = kzalloc(sizeof(*target), GFP_KERNEL);
  1911. if (!target) {
  1912. ath6kl_err("unable to allocate memory\n");
  1913. return NULL;
  1914. }
  1915. target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
  1916. if (!target->dev) {
  1917. ath6kl_err("unable to allocate memory\n");
  1918. status = -ENOMEM;
  1919. goto fail_create_htc;
  1920. }
  1921. spin_lock_init(&target->htc_lock);
  1922. spin_lock_init(&target->rx_lock);
  1923. spin_lock_init(&target->tx_lock);
  1924. INIT_LIST_HEAD(&target->free_ctrl_txbuf);
  1925. INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
  1926. INIT_LIST_HEAD(&target->cred_dist_list);
  1927. target->dev->ar = ar;
  1928. target->dev->htc_cnxt = target;
  1929. target->ep_waiting = ENDPOINT_MAX;
  1930. reset_ep_state(target);
  1931. status = ath6kldev_setup(target->dev);
  1932. if (status)
  1933. goto fail_create_htc;
  1934. block_size = ar->mbox_info.block_size;
  1935. ctrl_bufsz = (block_size > HTC_MAX_CTRL_MSG_LEN) ?
  1936. (block_size + HTC_HDR_LENGTH) :
  1937. (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH);
  1938. for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
  1939. packet = kzalloc(sizeof(*packet), GFP_KERNEL);
  1940. if (!packet)
  1941. break;
  1942. packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
  1943. if (!packet->buf_start) {
  1944. kfree(packet);
  1945. break;
  1946. }
  1947. packet->buf_len = ctrl_bufsz;
  1948. if (i < NUM_CONTROL_RX_BUFFERS) {
  1949. packet->act_len = 0;
  1950. packet->buf = packet->buf_start;
  1951. packet->endpoint = ENDPOINT_0;
  1952. list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
  1953. } else
  1954. list_add_tail(&packet->list, &target->free_ctrl_txbuf);
  1955. }
  1956. fail_create_htc:
  1957. if (i != NUM_CONTROL_BUFFERS || status) {
  1958. if (target) {
  1959. ath6kl_htc_cleanup(target);
  1960. target = NULL;
  1961. }
  1962. }
  1963. return target;
  1964. }
  1965. /* cleanup the HTC instance */
  1966. void ath6kl_htc_cleanup(struct htc_target *target)
  1967. {
  1968. struct htc_packet *packet, *tmp_packet;
  1969. ath6kl_hif_cleanup_scatter(target->dev->ar);
  1970. list_for_each_entry_safe(packet, tmp_packet,
  1971. &target->free_ctrl_txbuf, list) {
  1972. list_del(&packet->list);
  1973. kfree(packet->buf_start);
  1974. kfree(packet);
  1975. }
  1976. list_for_each_entry_safe(packet, tmp_packet,
  1977. &target->free_ctrl_rxbuf, list) {
  1978. list_del(&packet->list);
  1979. kfree(packet->buf_start);
  1980. kfree(packet);
  1981. }
  1982. kfree(target->dev);
  1983. kfree(target);
  1984. }