htc.c 61 KB

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