htc.c 61 KB

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