htc.c 73 KB

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