htc.c 62 KB

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