htc_mbox.c 75 KB

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