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