htc.c 62 KB

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