htc.c 62 KB

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