htc.c 69 KB

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