htc.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889
  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(void *htc_handle,
  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_handle, 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_dist_list;
  147. list_for_each_entry(cur_dist_list, epdist_list, list) {
  148. if (cur_dist_list->endpoint == ENDPOINT_0)
  149. continue;
  150. if (cur_dist_list->cred_to_dist > 0) {
  151. cur_dist_list->credits +=
  152. cur_dist_list->cred_to_dist;
  153. cur_dist_list->cred_to_dist = 0;
  154. if (cur_dist_list->credits >
  155. cur_dist_list->cred_assngd)
  156. ath6kl_credit_reduce(cred_info,
  157. cur_dist_list,
  158. cur_dist_list->cred_assngd);
  159. if (cur_dist_list->credits >
  160. cur_dist_list->cred_norm)
  161. ath6kl_credit_reduce(cred_info, cur_dist_list,
  162. cur_dist_list->cred_norm);
  163. if (!(cur_dist_list->dist_flags & HTC_EP_ACTIVE)) {
  164. if (cur_dist_list->txq_depth == 0)
  165. ath6kl_credit_reduce(cred_info,
  166. cur_dist_list, 0);
  167. }
  168. }
  169. }
  170. }
  171. /*
  172. * HTC has an endpoint that needs credits, ep_dist is the endpoint in
  173. * question.
  174. */
  175. static void ath6kl_credit_seek(struct ath6kl_htc_credit_info *cred_info,
  176. struct htc_endpoint_credit_dist *ep_dist)
  177. {
  178. struct htc_endpoint_credit_dist *curdist_list;
  179. int credits = 0;
  180. int need;
  181. if (ep_dist->svc_id == WMI_CONTROL_SVC)
  182. goto out;
  183. if ((ep_dist->svc_id == WMI_DATA_VI_SVC) ||
  184. (ep_dist->svc_id == WMI_DATA_VO_SVC))
  185. if ((ep_dist->cred_assngd >= ep_dist->cred_norm))
  186. goto out;
  187. /*
  188. * For all other services, we follow a simple algorithm of:
  189. *
  190. * 1. checking the free pool for credits
  191. * 2. checking lower priority endpoints for credits to take
  192. */
  193. credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
  194. if (credits >= ep_dist->seek_cred)
  195. goto out;
  196. /*
  197. * We don't have enough in the free pool, try taking away from
  198. * lower priority services The rule for taking away credits:
  199. *
  200. * 1. Only take from lower priority endpoints
  201. * 2. Only take what is allocated above the minimum (never
  202. * starve an endpoint completely)
  203. * 3. Only take what you need.
  204. */
  205. list_for_each_entry_reverse(curdist_list,
  206. &cred_info->lowestpri_ep_dist,
  207. list) {
  208. if (curdist_list == ep_dist)
  209. break;
  210. need = ep_dist->seek_cred - cred_info->cur_free_credits;
  211. if ((curdist_list->cred_assngd - need) >=
  212. curdist_list->cred_min) {
  213. /*
  214. * The current one has been allocated more than
  215. * it's minimum and it has enough credits assigned
  216. * above it's minimum to fulfill our need try to
  217. * take away just enough to fulfill our need.
  218. */
  219. ath6kl_credit_reduce(cred_info, curdist_list,
  220. curdist_list->cred_assngd - need);
  221. if (cred_info->cur_free_credits >=
  222. ep_dist->seek_cred)
  223. break;
  224. }
  225. if (curdist_list->endpoint == ENDPOINT_0)
  226. break;
  227. }
  228. credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
  229. out:
  230. /* did we find some credits? */
  231. if (credits)
  232. ath6kl_credit_deposit(cred_info, ep_dist, credits);
  233. ep_dist->seek_cred = 0;
  234. }
  235. /* redistribute credits based on activity change */
  236. static void ath6kl_credit_redistribute(struct ath6kl_htc_credit_info *info,
  237. struct list_head *ep_dist_list)
  238. {
  239. struct htc_endpoint_credit_dist *curdist_list;
  240. list_for_each_entry(curdist_list, ep_dist_list, list) {
  241. if (curdist_list->endpoint == ENDPOINT_0)
  242. continue;
  243. if ((curdist_list->svc_id == WMI_DATA_BK_SVC) ||
  244. (curdist_list->svc_id == WMI_DATA_BE_SVC))
  245. curdist_list->dist_flags |= HTC_EP_ACTIVE;
  246. if ((curdist_list->svc_id != WMI_CONTROL_SVC) &&
  247. !(curdist_list->dist_flags & HTC_EP_ACTIVE)) {
  248. if (curdist_list->txq_depth == 0)
  249. ath6kl_credit_reduce(info, curdist_list, 0);
  250. else
  251. ath6kl_credit_reduce(info,
  252. curdist_list,
  253. curdist_list->cred_min);
  254. }
  255. }
  256. }
  257. /*
  258. *
  259. * This function is invoked whenever endpoints require credit
  260. * distributions. A lock is held while this function is invoked, this
  261. * function shall NOT block. The ep_dist_list is a list of distribution
  262. * structures in prioritized order as defined by the call to the
  263. * htc_set_credit_dist() api.
  264. */
  265. static void ath6kl_credit_distribute(struct ath6kl_htc_credit_info *cred_info,
  266. struct list_head *ep_dist_list,
  267. enum htc_credit_dist_reason reason)
  268. {
  269. switch (reason) {
  270. case HTC_CREDIT_DIST_SEND_COMPLETE:
  271. ath6kl_credit_update(cred_info, ep_dist_list);
  272. break;
  273. case HTC_CREDIT_DIST_ACTIVITY_CHANGE:
  274. ath6kl_credit_redistribute(cred_info, ep_dist_list);
  275. break;
  276. default:
  277. break;
  278. }
  279. WARN_ON(cred_info->cur_free_credits > cred_info->total_avail_credits);
  280. WARN_ON(cred_info->cur_free_credits < 0);
  281. }
  282. static void ath6kl_htc_tx_buf_align(u8 **buf, unsigned long len)
  283. {
  284. u8 *align_addr;
  285. if (!IS_ALIGNED((unsigned long) *buf, 4)) {
  286. align_addr = PTR_ALIGN(*buf - 4, 4);
  287. memmove(align_addr, *buf, len);
  288. *buf = align_addr;
  289. }
  290. }
  291. static void ath6kl_htc_tx_prep_pkt(struct htc_packet *packet, u8 flags,
  292. int ctrl0, int ctrl1)
  293. {
  294. struct htc_frame_hdr *hdr;
  295. packet->buf -= HTC_HDR_LENGTH;
  296. hdr = (struct htc_frame_hdr *)packet->buf;
  297. /* Endianess? */
  298. put_unaligned((u16)packet->act_len, &hdr->payld_len);
  299. hdr->flags = flags;
  300. hdr->eid = packet->endpoint;
  301. hdr->ctrl[0] = ctrl0;
  302. hdr->ctrl[1] = ctrl1;
  303. }
  304. static void htc_reclaim_txctrl_buf(struct htc_target *target,
  305. struct htc_packet *pkt)
  306. {
  307. spin_lock_bh(&target->htc_lock);
  308. list_add_tail(&pkt->list, &target->free_ctrl_txbuf);
  309. spin_unlock_bh(&target->htc_lock);
  310. }
  311. static struct htc_packet *htc_get_control_buf(struct htc_target *target,
  312. bool tx)
  313. {
  314. struct htc_packet *packet = NULL;
  315. struct list_head *buf_list;
  316. buf_list = tx ? &target->free_ctrl_txbuf : &target->free_ctrl_rxbuf;
  317. spin_lock_bh(&target->htc_lock);
  318. if (list_empty(buf_list)) {
  319. spin_unlock_bh(&target->htc_lock);
  320. return NULL;
  321. }
  322. packet = list_first_entry(buf_list, struct htc_packet, list);
  323. list_del(&packet->list);
  324. spin_unlock_bh(&target->htc_lock);
  325. if (tx)
  326. packet->buf = packet->buf_start + HTC_HDR_LENGTH;
  327. return packet;
  328. }
  329. static void htc_tx_comp_update(struct htc_target *target,
  330. struct htc_endpoint *endpoint,
  331. struct htc_packet *packet)
  332. {
  333. packet->completion = NULL;
  334. packet->buf += HTC_HDR_LENGTH;
  335. if (!packet->status)
  336. return;
  337. ath6kl_err("req failed (status:%d, ep:%d, len:%d creds:%d)\n",
  338. packet->status, packet->endpoint, packet->act_len,
  339. packet->info.tx.cred_used);
  340. /* on failure to submit, reclaim credits for this packet */
  341. spin_lock_bh(&target->tx_lock);
  342. endpoint->cred_dist.cred_to_dist +=
  343. packet->info.tx.cred_used;
  344. endpoint->cred_dist.txq_depth = get_queue_depth(&endpoint->txq);
  345. ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx ctxt 0x%p dist 0x%p\n",
  346. target->credit_info, &target->cred_dist_list);
  347. ath6kl_credit_distribute(target->credit_info,
  348. &target->cred_dist_list,
  349. HTC_CREDIT_DIST_SEND_COMPLETE);
  350. spin_unlock_bh(&target->tx_lock);
  351. }
  352. static void htc_tx_complete(struct htc_endpoint *endpoint,
  353. struct list_head *txq)
  354. {
  355. if (list_empty(txq))
  356. return;
  357. ath6kl_dbg(ATH6KL_DBG_HTC,
  358. "htc tx complete ep %d pkts %d\n",
  359. endpoint->eid, get_queue_depth(txq));
  360. ath6kl_tx_complete(endpoint->target->dev->ar, txq);
  361. }
  362. static void htc_tx_comp_handler(struct htc_target *target,
  363. struct htc_packet *packet)
  364. {
  365. struct htc_endpoint *endpoint = &target->endpoint[packet->endpoint];
  366. struct list_head container;
  367. ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx complete seqno %d\n",
  368. packet->info.tx.seqno);
  369. htc_tx_comp_update(target, endpoint, packet);
  370. INIT_LIST_HEAD(&container);
  371. list_add_tail(&packet->list, &container);
  372. /* do completion */
  373. htc_tx_complete(endpoint, &container);
  374. }
  375. static void htc_async_tx_scat_complete(struct htc_target *target,
  376. struct hif_scatter_req *scat_req)
  377. {
  378. struct htc_endpoint *endpoint;
  379. struct htc_packet *packet;
  380. struct list_head tx_compq;
  381. int i;
  382. INIT_LIST_HEAD(&tx_compq);
  383. ath6kl_dbg(ATH6KL_DBG_HTC,
  384. "htc tx scat complete len %d entries %d\n",
  385. scat_req->len, scat_req->scat_entries);
  386. if (scat_req->status)
  387. ath6kl_err("send scatter req failed: %d\n", scat_req->status);
  388. packet = scat_req->scat_list[0].packet;
  389. endpoint = &target->endpoint[packet->endpoint];
  390. /* walk through the scatter list and process */
  391. for (i = 0; i < scat_req->scat_entries; i++) {
  392. packet = scat_req->scat_list[i].packet;
  393. if (!packet) {
  394. WARN_ON(1);
  395. return;
  396. }
  397. packet->status = scat_req->status;
  398. htc_tx_comp_update(target, endpoint, packet);
  399. list_add_tail(&packet->list, &tx_compq);
  400. }
  401. /* free scatter request */
  402. hif_scatter_req_add(target->dev->ar, scat_req);
  403. /* complete all packets */
  404. htc_tx_complete(endpoint, &tx_compq);
  405. }
  406. static int ath6kl_htc_tx_issue(struct htc_target *target,
  407. struct htc_packet *packet)
  408. {
  409. int status;
  410. bool sync = false;
  411. u32 padded_len, send_len;
  412. if (!packet->completion)
  413. sync = true;
  414. send_len = packet->act_len + HTC_HDR_LENGTH;
  415. padded_len = CALC_TXRX_PADDED_LEN(target, send_len);
  416. ath6kl_dbg(ATH6KL_DBG_HTC,
  417. "htc tx issue len %d seqno %d padded_len %d mbox 0x%X %s\n",
  418. send_len, packet->info.tx.seqno, padded_len,
  419. target->dev->ar->mbox_info.htc_addr,
  420. sync ? "sync" : "async");
  421. if (sync) {
  422. status = hif_read_write_sync(target->dev->ar,
  423. target->dev->ar->mbox_info.htc_addr,
  424. packet->buf, padded_len,
  425. HIF_WR_SYNC_BLOCK_INC);
  426. packet->status = status;
  427. packet->buf += HTC_HDR_LENGTH;
  428. } else
  429. status = hif_write_async(target->dev->ar,
  430. target->dev->ar->mbox_info.htc_addr,
  431. packet->buf, padded_len,
  432. HIF_WR_ASYNC_BLOCK_INC, packet);
  433. return status;
  434. }
  435. static int htc_check_credits(struct htc_target *target,
  436. struct htc_endpoint *ep, u8 *flags,
  437. enum htc_endpoint_id eid, unsigned int len,
  438. int *req_cred)
  439. {
  440. *req_cred = (len > target->tgt_cred_sz) ?
  441. DIV_ROUND_UP(len, target->tgt_cred_sz) : 1;
  442. ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit check need %d got %d\n",
  443. *req_cred, ep->cred_dist.credits);
  444. if (ep->cred_dist.credits < *req_cred) {
  445. if (eid == ENDPOINT_0)
  446. return -EINVAL;
  447. /* Seek more credits */
  448. ep->cred_dist.seek_cred = *req_cred - ep->cred_dist.credits;
  449. ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
  450. ep->cred_dist.seek_cred = 0;
  451. if (ep->cred_dist.credits < *req_cred) {
  452. ath6kl_dbg(ATH6KL_DBG_CREDIT,
  453. "credit not found for ep %d\n",
  454. eid);
  455. return -EINVAL;
  456. }
  457. }
  458. ep->cred_dist.credits -= *req_cred;
  459. ep->ep_st.cred_cosumd += *req_cred;
  460. /* When we are getting low on credits, ask for more */
  461. if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
  462. ep->cred_dist.seek_cred =
  463. ep->cred_dist.cred_per_msg - ep->cred_dist.credits;
  464. ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
  465. /* see if we were successful in getting more */
  466. if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
  467. /* tell the target we need credits ASAP! */
  468. *flags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  469. ep->ep_st.cred_low_indicate += 1;
  470. ath6kl_dbg(ATH6KL_DBG_CREDIT,
  471. "credit we need credits asap\n");
  472. }
  473. }
  474. return 0;
  475. }
  476. static void ath6kl_htc_tx_pkts_get(struct htc_target *target,
  477. struct htc_endpoint *endpoint,
  478. struct list_head *queue)
  479. {
  480. int req_cred;
  481. u8 flags;
  482. struct htc_packet *packet;
  483. unsigned int len;
  484. while (true) {
  485. flags = 0;
  486. if (list_empty(&endpoint->txq))
  487. break;
  488. packet = list_first_entry(&endpoint->txq, struct htc_packet,
  489. list);
  490. ath6kl_dbg(ATH6KL_DBG_HTC,
  491. "htc tx got packet 0x%p queue depth %d\n",
  492. packet, get_queue_depth(&endpoint->txq));
  493. len = CALC_TXRX_PADDED_LEN(target,
  494. packet->act_len + HTC_HDR_LENGTH);
  495. if (htc_check_credits(target, endpoint, &flags,
  496. packet->endpoint, len, &req_cred))
  497. break;
  498. /* now we can fully move onto caller's queue */
  499. packet = list_first_entry(&endpoint->txq, struct htc_packet,
  500. list);
  501. list_move_tail(&packet->list, queue);
  502. /* save the number of credits this packet consumed */
  503. packet->info.tx.cred_used = req_cred;
  504. /* all TX packets are handled asynchronously */
  505. packet->completion = htc_tx_comp_handler;
  506. packet->context = target;
  507. endpoint->ep_st.tx_issued += 1;
  508. /* save send flags */
  509. packet->info.tx.flags = flags;
  510. packet->info.tx.seqno = endpoint->seqno;
  511. endpoint->seqno++;
  512. }
  513. }
  514. /* See if the padded tx length falls on a credit boundary */
  515. static int htc_get_credit_padding(unsigned int cred_sz, int *len,
  516. struct htc_endpoint *ep)
  517. {
  518. int rem_cred, cred_pad;
  519. rem_cred = *len % cred_sz;
  520. /* No padding needed */
  521. if (!rem_cred)
  522. return 0;
  523. if (!(ep->conn_flags & HTC_FLGS_TX_BNDL_PAD_EN))
  524. return -1;
  525. /*
  526. * The transfer consumes a "partial" credit, this
  527. * packet cannot be bundled unless we add
  528. * additional "dummy" padding (max 255 bytes) to
  529. * consume the entire credit.
  530. */
  531. cred_pad = *len < cred_sz ? (cred_sz - *len) : rem_cred;
  532. if ((cred_pad > 0) && (cred_pad <= 255))
  533. *len += cred_pad;
  534. else
  535. /* The amount of padding is too large, send as non-bundled */
  536. return -1;
  537. return cred_pad;
  538. }
  539. static int ath6kl_htc_tx_setup_scat_list(struct htc_target *target,
  540. struct htc_endpoint *endpoint,
  541. struct hif_scatter_req *scat_req,
  542. int n_scat,
  543. struct list_head *queue)
  544. {
  545. struct htc_packet *packet;
  546. int i, len, rem_scat, cred_pad;
  547. int status = 0;
  548. rem_scat = target->max_tx_bndl_sz;
  549. for (i = 0; i < n_scat; i++) {
  550. scat_req->scat_list[i].packet = NULL;
  551. if (list_empty(queue))
  552. break;
  553. packet = list_first_entry(queue, struct htc_packet, list);
  554. len = CALC_TXRX_PADDED_LEN(target,
  555. packet->act_len + HTC_HDR_LENGTH);
  556. cred_pad = htc_get_credit_padding(target->tgt_cred_sz,
  557. &len, endpoint);
  558. if (cred_pad < 0 || rem_scat < len) {
  559. status = -ENOSPC;
  560. break;
  561. }
  562. rem_scat -= len;
  563. /* now remove it from the queue */
  564. list_del(&packet->list);
  565. scat_req->scat_list[i].packet = packet;
  566. /* prepare packet and flag message as part of a send bundle */
  567. ath6kl_htc_tx_prep_pkt(packet,
  568. packet->info.tx.flags | HTC_FLAGS_SEND_BUNDLE,
  569. cred_pad, packet->info.tx.seqno);
  570. /* Make sure the buffer is 4-byte aligned */
  571. ath6kl_htc_tx_buf_align(&packet->buf,
  572. packet->act_len + HTC_HDR_LENGTH);
  573. scat_req->scat_list[i].buf = packet->buf;
  574. scat_req->scat_list[i].len = len;
  575. scat_req->len += len;
  576. scat_req->scat_entries++;
  577. ath6kl_dbg(ATH6KL_DBG_HTC,
  578. "htc tx adding (%d) pkt 0x%p seqno %d len %d remaining %d\n",
  579. i, packet, packet->info.tx.seqno, len, rem_scat);
  580. }
  581. /* Roll back scatter setup in case of any failure */
  582. if (scat_req->scat_entries < HTC_MIN_HTC_MSGS_TO_BUNDLE) {
  583. for (i = scat_req->scat_entries - 1; i >= 0; i--) {
  584. packet = scat_req->scat_list[i].packet;
  585. if (packet) {
  586. packet->buf += HTC_HDR_LENGTH;
  587. list_add(&packet->list, queue);
  588. }
  589. }
  590. return -EAGAIN;
  591. }
  592. return status;
  593. }
  594. /*
  595. * Drain a queue and send as bundles this function may return without fully
  596. * draining the queue when
  597. *
  598. * 1. scatter resources are exhausted
  599. * 2. a message that will consume a partial credit will stop the
  600. * bundling process early
  601. * 3. we drop below the minimum number of messages for a bundle
  602. */
  603. static void ath6kl_htc_tx_bundle(struct htc_endpoint *endpoint,
  604. struct list_head *queue,
  605. int *sent_bundle, int *n_bundle_pkts)
  606. {
  607. struct htc_target *target = endpoint->target;
  608. struct hif_scatter_req *scat_req = NULL;
  609. int n_scat, n_sent_bundle = 0, tot_pkts_bundle = 0;
  610. int status;
  611. u32 txb_mask;
  612. u8 ac = WMM_NUM_AC;
  613. if ((HTC_CTRL_RSVD_SVC != endpoint->svc_id) ||
  614. (WMI_CONTROL_SVC != endpoint->svc_id))
  615. ac = target->dev->ar->ep2ac_map[endpoint->eid];
  616. while (true) {
  617. status = 0;
  618. n_scat = get_queue_depth(queue);
  619. n_scat = min(n_scat, target->msg_per_bndl_max);
  620. if (n_scat < HTC_MIN_HTC_MSGS_TO_BUNDLE)
  621. /* not enough to bundle */
  622. break;
  623. scat_req = hif_scatter_req_get(target->dev->ar);
  624. if (!scat_req) {
  625. /* no scatter resources */
  626. ath6kl_dbg(ATH6KL_DBG_HTC,
  627. "htc tx no more scatter resources\n");
  628. break;
  629. }
  630. if ((ac < WMM_NUM_AC) && (ac != WMM_AC_BK)) {
  631. if (WMM_AC_BE == ac)
  632. /*
  633. * BE, BK have priorities and bit
  634. * positions reversed
  635. */
  636. txb_mask = (1 << WMM_AC_BK);
  637. else
  638. /*
  639. * any AC with priority lower than
  640. * itself
  641. */
  642. txb_mask = ((1 << ac) - 1);
  643. /*
  644. * when the scatter request resources drop below a
  645. * certain threshold, disable Tx bundling for all
  646. * AC's with priority lower than the current requesting
  647. * AC. Otherwise re-enable Tx bundling for them
  648. */
  649. if (scat_req->scat_q_depth < ATH6KL_SCATTER_REQS)
  650. target->tx_bndl_mask &= ~txb_mask;
  651. else
  652. target->tx_bndl_mask |= txb_mask;
  653. }
  654. ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx pkts to scatter: %d\n",
  655. n_scat);
  656. scat_req->len = 0;
  657. scat_req->scat_entries = 0;
  658. status = ath6kl_htc_tx_setup_scat_list(target, endpoint,
  659. scat_req, n_scat,
  660. queue);
  661. if (status == -EAGAIN) {
  662. hif_scatter_req_add(target->dev->ar, scat_req);
  663. break;
  664. }
  665. /* send path is always asynchronous */
  666. scat_req->complete = htc_async_tx_scat_complete;
  667. n_sent_bundle++;
  668. tot_pkts_bundle += scat_req->scat_entries;
  669. ath6kl_dbg(ATH6KL_DBG_HTC,
  670. "htc tx scatter bytes %d entries %d\n",
  671. scat_req->len, scat_req->scat_entries);
  672. ath6kl_hif_submit_scat_req(target->dev, scat_req, false);
  673. if (status)
  674. break;
  675. }
  676. *sent_bundle = n_sent_bundle;
  677. *n_bundle_pkts = tot_pkts_bundle;
  678. ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx bundle sent %d pkts\n",
  679. n_sent_bundle);
  680. return;
  681. }
  682. static void ath6kl_htc_tx_from_queue(struct htc_target *target,
  683. struct htc_endpoint *endpoint)
  684. {
  685. struct list_head txq;
  686. struct htc_packet *packet;
  687. int bundle_sent;
  688. int n_pkts_bundle;
  689. u8 ac = WMM_NUM_AC;
  690. spin_lock_bh(&target->tx_lock);
  691. endpoint->tx_proc_cnt++;
  692. if (endpoint->tx_proc_cnt > 1) {
  693. endpoint->tx_proc_cnt--;
  694. spin_unlock_bh(&target->tx_lock);
  695. ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx busy\n");
  696. return;
  697. }
  698. /*
  699. * drain the endpoint TX queue for transmission as long
  700. * as we have enough credits.
  701. */
  702. INIT_LIST_HEAD(&txq);
  703. if ((HTC_CTRL_RSVD_SVC != endpoint->svc_id) ||
  704. (WMI_CONTROL_SVC != endpoint->svc_id))
  705. ac = target->dev->ar->ep2ac_map[endpoint->eid];
  706. while (true) {
  707. if (list_empty(&endpoint->txq))
  708. break;
  709. ath6kl_htc_tx_pkts_get(target, endpoint, &txq);
  710. if (list_empty(&txq))
  711. break;
  712. spin_unlock_bh(&target->tx_lock);
  713. bundle_sent = 0;
  714. n_pkts_bundle = 0;
  715. while (true) {
  716. /* try to send a bundle on each pass */
  717. if ((target->tx_bndl_mask) &&
  718. (get_queue_depth(&txq) >=
  719. HTC_MIN_HTC_MSGS_TO_BUNDLE)) {
  720. int temp1 = 0, temp2 = 0;
  721. /* check if bundling is enabled for an AC */
  722. if (target->tx_bndl_mask & (1 << ac)) {
  723. ath6kl_htc_tx_bundle(endpoint, &txq,
  724. &temp1, &temp2);
  725. bundle_sent += temp1;
  726. n_pkts_bundle += temp2;
  727. }
  728. }
  729. if (list_empty(&txq))
  730. break;
  731. packet = list_first_entry(&txq, struct htc_packet,
  732. list);
  733. list_del(&packet->list);
  734. ath6kl_htc_tx_prep_pkt(packet, packet->info.tx.flags,
  735. 0, packet->info.tx.seqno);
  736. ath6kl_htc_tx_issue(target, packet);
  737. }
  738. spin_lock_bh(&target->tx_lock);
  739. endpoint->ep_st.tx_bundles += bundle_sent;
  740. endpoint->ep_st.tx_pkt_bundled += n_pkts_bundle;
  741. /*
  742. * if an AC has bundling disabled and no tx bundling
  743. * has occured continously for a certain number of TX,
  744. * enable tx bundling for this AC
  745. */
  746. if (!bundle_sent) {
  747. if (!(target->tx_bndl_mask & (1 << ac)) &&
  748. (ac < WMM_NUM_AC)) {
  749. if (++target->ac_tx_count[ac] >=
  750. TX_RESUME_BUNDLE_THRESHOLD) {
  751. target->ac_tx_count[ac] = 0;
  752. target->tx_bndl_mask |= (1 << ac);
  753. }
  754. }
  755. } else {
  756. /* tx bundling will reset the counter */
  757. if (ac < WMM_NUM_AC)
  758. target->ac_tx_count[ac] = 0;
  759. }
  760. }
  761. endpoint->tx_proc_cnt = 0;
  762. spin_unlock_bh(&target->tx_lock);
  763. }
  764. static bool ath6kl_htc_tx_try(struct htc_target *target,
  765. struct htc_endpoint *endpoint,
  766. struct htc_packet *tx_pkt)
  767. {
  768. struct htc_ep_callbacks ep_cb;
  769. int txq_depth;
  770. bool overflow = false;
  771. ep_cb = endpoint->ep_cb;
  772. spin_lock_bh(&target->tx_lock);
  773. txq_depth = get_queue_depth(&endpoint->txq);
  774. spin_unlock_bh(&target->tx_lock);
  775. if (txq_depth >= endpoint->max_txq_depth)
  776. overflow = true;
  777. if (overflow)
  778. ath6kl_dbg(ATH6KL_DBG_HTC,
  779. "htc tx overflow ep %d depth %d max %d\n",
  780. endpoint->eid, txq_depth,
  781. endpoint->max_txq_depth);
  782. if (overflow && ep_cb.tx_full) {
  783. if (ep_cb.tx_full(endpoint->target, tx_pkt) ==
  784. HTC_SEND_FULL_DROP) {
  785. endpoint->ep_st.tx_dropped += 1;
  786. return false;
  787. }
  788. }
  789. spin_lock_bh(&target->tx_lock);
  790. list_add_tail(&tx_pkt->list, &endpoint->txq);
  791. spin_unlock_bh(&target->tx_lock);
  792. ath6kl_htc_tx_from_queue(target, endpoint);
  793. return true;
  794. }
  795. static void htc_chk_ep_txq(struct htc_target *target)
  796. {
  797. struct htc_endpoint *endpoint;
  798. struct htc_endpoint_credit_dist *cred_dist;
  799. /*
  800. * Run through the credit distribution list to see if there are
  801. * packets queued. NOTE: no locks need to be taken since the
  802. * distribution list is not dynamic (cannot be re-ordered) and we
  803. * are not modifying any state.
  804. */
  805. list_for_each_entry(cred_dist, &target->cred_dist_list, list) {
  806. endpoint = cred_dist->htc_ep;
  807. spin_lock_bh(&target->tx_lock);
  808. if (!list_empty(&endpoint->txq)) {
  809. ath6kl_dbg(ATH6KL_DBG_HTC,
  810. "htc creds ep %d credits %d pkts %d\n",
  811. cred_dist->endpoint,
  812. endpoint->cred_dist.credits,
  813. get_queue_depth(&endpoint->txq));
  814. spin_unlock_bh(&target->tx_lock);
  815. /*
  816. * Try to start the stalled queue, this list is
  817. * ordered by priority. If there are credits
  818. * available the highest priority queue will get a
  819. * chance to reclaim credits from lower priority
  820. * ones.
  821. */
  822. ath6kl_htc_tx_from_queue(target, endpoint);
  823. spin_lock_bh(&target->tx_lock);
  824. }
  825. spin_unlock_bh(&target->tx_lock);
  826. }
  827. }
  828. static int htc_setup_tx_complete(struct htc_target *target)
  829. {
  830. struct htc_packet *send_pkt = NULL;
  831. int status;
  832. send_pkt = htc_get_control_buf(target, true);
  833. if (!send_pkt)
  834. return -ENOMEM;
  835. if (target->htc_tgt_ver >= HTC_VERSION_2P1) {
  836. struct htc_setup_comp_ext_msg *setup_comp_ext;
  837. u32 flags = 0;
  838. setup_comp_ext =
  839. (struct htc_setup_comp_ext_msg *)send_pkt->buf;
  840. memset(setup_comp_ext, 0, sizeof(*setup_comp_ext));
  841. setup_comp_ext->msg_id =
  842. cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);
  843. if (target->msg_per_bndl_max > 0) {
  844. /* Indicate HTC bundling to the target */
  845. flags |= HTC_SETUP_COMP_FLG_RX_BNDL_EN;
  846. setup_comp_ext->msg_per_rxbndl =
  847. target->msg_per_bndl_max;
  848. }
  849. memcpy(&setup_comp_ext->flags, &flags,
  850. sizeof(setup_comp_ext->flags));
  851. set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp_ext,
  852. sizeof(struct htc_setup_comp_ext_msg),
  853. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  854. } else {
  855. struct htc_setup_comp_msg *setup_comp;
  856. setup_comp = (struct htc_setup_comp_msg *)send_pkt->buf;
  857. memset(setup_comp, 0, sizeof(struct htc_setup_comp_msg));
  858. setup_comp->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_ID);
  859. set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp,
  860. sizeof(struct htc_setup_comp_msg),
  861. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  862. }
  863. /* we want synchronous operation */
  864. send_pkt->completion = NULL;
  865. ath6kl_htc_tx_prep_pkt(send_pkt, 0, 0, 0);
  866. status = ath6kl_htc_tx_issue(target, send_pkt);
  867. if (send_pkt != NULL)
  868. htc_reclaim_txctrl_buf(target, send_pkt);
  869. return status;
  870. }
  871. void ath6kl_htc_set_credit_dist(struct htc_target *target,
  872. struct ath6kl_htc_credit_info *credit_info,
  873. u16 srvc_pri_order[], int list_len)
  874. {
  875. struct htc_endpoint *endpoint;
  876. int i, ep;
  877. target->credit_info = credit_info;
  878. list_add_tail(&target->endpoint[ENDPOINT_0].cred_dist.list,
  879. &target->cred_dist_list);
  880. for (i = 0; i < list_len; i++) {
  881. for (ep = ENDPOINT_1; ep < ENDPOINT_MAX; ep++) {
  882. endpoint = &target->endpoint[ep];
  883. if (endpoint->svc_id == srvc_pri_order[i]) {
  884. list_add_tail(&endpoint->cred_dist.list,
  885. &target->cred_dist_list);
  886. break;
  887. }
  888. }
  889. if (ep >= ENDPOINT_MAX) {
  890. WARN_ON(1);
  891. return;
  892. }
  893. }
  894. }
  895. int ath6kl_htc_tx(struct htc_target *target, struct htc_packet *packet)
  896. {
  897. struct htc_endpoint *endpoint;
  898. struct list_head queue;
  899. ath6kl_dbg(ATH6KL_DBG_HTC,
  900. "htc tx ep id %d buf 0x%p len %d\n",
  901. packet->endpoint, packet->buf, packet->act_len);
  902. if (packet->endpoint >= ENDPOINT_MAX) {
  903. WARN_ON(1);
  904. return -EINVAL;
  905. }
  906. endpoint = &target->endpoint[packet->endpoint];
  907. if (!ath6kl_htc_tx_try(target, endpoint, packet)) {
  908. packet->status = (target->htc_flags & HTC_OP_STATE_STOPPING) ?
  909. -ECANCELED : -ENOSPC;
  910. INIT_LIST_HEAD(&queue);
  911. list_add(&packet->list, &queue);
  912. htc_tx_complete(endpoint, &queue);
  913. }
  914. return 0;
  915. }
  916. /* flush endpoint TX queue */
  917. void ath6kl_htc_flush_txep(struct htc_target *target,
  918. enum htc_endpoint_id eid, u16 tag)
  919. {
  920. struct htc_packet *packet, *tmp_pkt;
  921. struct list_head discard_q, container;
  922. struct htc_endpoint *endpoint = &target->endpoint[eid];
  923. if (!endpoint->svc_id) {
  924. WARN_ON(1);
  925. return;
  926. }
  927. /* initialize the discard queue */
  928. INIT_LIST_HEAD(&discard_q);
  929. spin_lock_bh(&target->tx_lock);
  930. list_for_each_entry_safe(packet, tmp_pkt, &endpoint->txq, list) {
  931. if ((tag == HTC_TX_PACKET_TAG_ALL) ||
  932. (tag == packet->info.tx.tag))
  933. list_move_tail(&packet->list, &discard_q);
  934. }
  935. spin_unlock_bh(&target->tx_lock);
  936. list_for_each_entry_safe(packet, tmp_pkt, &discard_q, list) {
  937. packet->status = -ECANCELED;
  938. list_del(&packet->list);
  939. ath6kl_dbg(ATH6KL_DBG_HTC,
  940. "htc tx flushing pkt 0x%p len %d ep %d tag 0x%x\n",
  941. packet, packet->act_len,
  942. packet->endpoint, packet->info.tx.tag);
  943. INIT_LIST_HEAD(&container);
  944. list_add_tail(&packet->list, &container);
  945. htc_tx_complete(endpoint, &container);
  946. }
  947. }
  948. static void ath6kl_htc_flush_txep_all(struct htc_target *target)
  949. {
  950. struct htc_endpoint *endpoint;
  951. int i;
  952. dump_cred_dist_stats(target);
  953. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  954. endpoint = &target->endpoint[i];
  955. if (endpoint->svc_id == 0)
  956. /* not in use.. */
  957. continue;
  958. ath6kl_htc_flush_txep(target, i, HTC_TX_PACKET_TAG_ALL);
  959. }
  960. }
  961. void ath6kl_htc_indicate_activity_change(struct htc_target *target,
  962. enum htc_endpoint_id eid, bool active)
  963. {
  964. struct htc_endpoint *endpoint = &target->endpoint[eid];
  965. bool dist = false;
  966. if (endpoint->svc_id == 0) {
  967. WARN_ON(1);
  968. return;
  969. }
  970. spin_lock_bh(&target->tx_lock);
  971. if (active) {
  972. if (!(endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE)) {
  973. endpoint->cred_dist.dist_flags |= HTC_EP_ACTIVE;
  974. dist = true;
  975. }
  976. } else {
  977. if (endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE) {
  978. endpoint->cred_dist.dist_flags &= ~HTC_EP_ACTIVE;
  979. dist = true;
  980. }
  981. }
  982. if (dist) {
  983. endpoint->cred_dist.txq_depth =
  984. get_queue_depth(&endpoint->txq);
  985. ath6kl_dbg(ATH6KL_DBG_HTC,
  986. "htc tx activity ctxt 0x%p dist 0x%p\n",
  987. target->credit_info, &target->cred_dist_list);
  988. ath6kl_credit_distribute(target->credit_info,
  989. &target->cred_dist_list,
  990. HTC_CREDIT_DIST_ACTIVITY_CHANGE);
  991. }
  992. spin_unlock_bh(&target->tx_lock);
  993. if (dist && !active)
  994. htc_chk_ep_txq(target);
  995. }
  996. /* HTC Rx */
  997. static inline void ath6kl_htc_rx_update_stats(struct htc_endpoint *endpoint,
  998. int n_look_ahds)
  999. {
  1000. endpoint->ep_st.rx_pkts++;
  1001. if (n_look_ahds == 1)
  1002. endpoint->ep_st.rx_lkahds++;
  1003. else if (n_look_ahds > 1)
  1004. endpoint->ep_st.rx_bundle_lkahd++;
  1005. }
  1006. static inline bool htc_valid_rx_frame_len(struct htc_target *target,
  1007. enum htc_endpoint_id eid, int len)
  1008. {
  1009. return (eid == target->dev->ar->ctrl_ep) ?
  1010. len <= ATH6KL_BUFFER_SIZE : len <= ATH6KL_AMSDU_BUFFER_SIZE;
  1011. }
  1012. static int htc_add_rxbuf(struct htc_target *target, struct htc_packet *packet)
  1013. {
  1014. struct list_head queue;
  1015. INIT_LIST_HEAD(&queue);
  1016. list_add_tail(&packet->list, &queue);
  1017. return ath6kl_htc_add_rxbuf_multiple(target, &queue);
  1018. }
  1019. static void htc_reclaim_rxbuf(struct htc_target *target,
  1020. struct htc_packet *packet,
  1021. struct htc_endpoint *ep)
  1022. {
  1023. if (packet->info.rx.rx_flags & HTC_RX_PKT_NO_RECYCLE) {
  1024. htc_rxpkt_reset(packet);
  1025. packet->status = -ECANCELED;
  1026. ep->ep_cb.rx(ep->target, packet);
  1027. } else {
  1028. htc_rxpkt_reset(packet);
  1029. htc_add_rxbuf((void *)(target), packet);
  1030. }
  1031. }
  1032. static void reclaim_rx_ctrl_buf(struct htc_target *target,
  1033. struct htc_packet *packet)
  1034. {
  1035. spin_lock_bh(&target->htc_lock);
  1036. list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
  1037. spin_unlock_bh(&target->htc_lock);
  1038. }
  1039. static int ath6kl_htc_rx_packet(struct htc_target *target,
  1040. struct htc_packet *packet,
  1041. u32 rx_len)
  1042. {
  1043. struct ath6kl_device *dev = target->dev;
  1044. u32 padded_len;
  1045. int status;
  1046. padded_len = CALC_TXRX_PADDED_LEN(target, rx_len);
  1047. if (padded_len > packet->buf_len) {
  1048. ath6kl_err("not enough receive space for packet - padlen %d recvlen %d bufferlen %d\n",
  1049. padded_len, rx_len, packet->buf_len);
  1050. return -ENOMEM;
  1051. }
  1052. ath6kl_dbg(ATH6KL_DBG_HTC,
  1053. "htc rx 0x%p hdr x%x len %d mbox 0x%x\n",
  1054. packet, packet->info.rx.exp_hdr,
  1055. padded_len, dev->ar->mbox_info.htc_addr);
  1056. status = hif_read_write_sync(dev->ar,
  1057. dev->ar->mbox_info.htc_addr,
  1058. packet->buf, padded_len,
  1059. HIF_RD_SYNC_BLOCK_FIX);
  1060. packet->status = status;
  1061. return status;
  1062. }
  1063. /*
  1064. * optimization for recv packets, we can indicate a
  1065. * "hint" that there are more single-packets to fetch
  1066. * on this endpoint.
  1067. */
  1068. static void ath6kl_htc_rx_set_indicate(u32 lk_ahd,
  1069. struct htc_endpoint *endpoint,
  1070. struct htc_packet *packet)
  1071. {
  1072. struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)&lk_ahd;
  1073. if (htc_hdr->eid == packet->endpoint) {
  1074. if (!list_empty(&endpoint->rx_bufq))
  1075. packet->info.rx.indicat_flags |=
  1076. HTC_RX_FLAGS_INDICATE_MORE_PKTS;
  1077. }
  1078. }
  1079. static void ath6kl_htc_rx_chk_water_mark(struct htc_endpoint *endpoint)
  1080. {
  1081. struct htc_ep_callbacks ep_cb = endpoint->ep_cb;
  1082. if (ep_cb.rx_refill_thresh > 0) {
  1083. spin_lock_bh(&endpoint->target->rx_lock);
  1084. if (get_queue_depth(&endpoint->rx_bufq)
  1085. < ep_cb.rx_refill_thresh) {
  1086. spin_unlock_bh(&endpoint->target->rx_lock);
  1087. ep_cb.rx_refill(endpoint->target, endpoint->eid);
  1088. return;
  1089. }
  1090. spin_unlock_bh(&endpoint->target->rx_lock);
  1091. }
  1092. }
  1093. /* This function is called with rx_lock held */
  1094. static int ath6kl_htc_rx_setup(struct htc_target *target,
  1095. struct htc_endpoint *ep,
  1096. u32 *lk_ahds, struct list_head *queue, int n_msg)
  1097. {
  1098. struct htc_packet *packet;
  1099. /* FIXME: type of lk_ahds can't be right */
  1100. struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)lk_ahds;
  1101. struct htc_ep_callbacks ep_cb;
  1102. int status = 0, j, full_len;
  1103. bool no_recycle;
  1104. full_len = CALC_TXRX_PADDED_LEN(target,
  1105. le16_to_cpu(htc_hdr->payld_len) +
  1106. sizeof(*htc_hdr));
  1107. if (!htc_valid_rx_frame_len(target, ep->eid, full_len)) {
  1108. ath6kl_warn("Rx buffer requested with invalid length\n");
  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. ath6kl_dbg(ATH6KL_DBG_HTC,
  1958. "htc connect service target 0x%p service id 0x%x\n",
  1959. target, conn_req->svc_id);
  1960. if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
  1961. /* special case for pseudo control service */
  1962. assigned_ep = ENDPOINT_0;
  1963. max_msg_sz = HTC_MAX_CTRL_MSG_LEN;
  1964. } else {
  1965. /* allocate a packet to send to the target */
  1966. tx_pkt = htc_get_control_buf(target, true);
  1967. if (!tx_pkt)
  1968. return -ENOMEM;
  1969. conn_msg = (struct htc_conn_service_msg *)tx_pkt->buf;
  1970. memset(conn_msg, 0, sizeof(*conn_msg));
  1971. conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
  1972. conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
  1973. conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags);
  1974. set_htc_pkt_info(tx_pkt, NULL, (u8 *) conn_msg,
  1975. sizeof(*conn_msg) + conn_msg->svc_meta_len,
  1976. ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
  1977. /* we want synchronous operation */
  1978. tx_pkt->completion = NULL;
  1979. ath6kl_htc_tx_prep_pkt(tx_pkt, 0, 0, 0);
  1980. status = ath6kl_htc_tx_issue(target, tx_pkt);
  1981. if (status)
  1982. goto fail_tx;
  1983. /* wait for response */
  1984. rx_pkt = htc_wait_for_ctrl_msg(target);
  1985. if (!rx_pkt) {
  1986. status = -ENOMEM;
  1987. goto fail_tx;
  1988. }
  1989. resp_msg = (struct htc_conn_service_resp *)rx_pkt->buf;
  1990. if ((le16_to_cpu(resp_msg->msg_id) != HTC_MSG_CONN_SVC_RESP_ID) ||
  1991. (rx_pkt->act_len < sizeof(*resp_msg))) {
  1992. status = -ENOMEM;
  1993. goto fail_tx;
  1994. }
  1995. conn_resp->resp_code = resp_msg->status;
  1996. /* check response status */
  1997. if (resp_msg->status != HTC_SERVICE_SUCCESS) {
  1998. ath6kl_err("target failed service 0x%X connect request (status:%d)\n",
  1999. resp_msg->svc_id, resp_msg->status);
  2000. status = -ENOMEM;
  2001. goto fail_tx;
  2002. }
  2003. assigned_ep = (enum htc_endpoint_id)resp_msg->eid;
  2004. max_msg_sz = le16_to_cpu(resp_msg->max_msg_sz);
  2005. }
  2006. if (assigned_ep >= ENDPOINT_MAX || !max_msg_sz) {
  2007. status = -ENOMEM;
  2008. goto fail_tx;
  2009. }
  2010. endpoint = &target->endpoint[assigned_ep];
  2011. endpoint->eid = assigned_ep;
  2012. if (endpoint->svc_id) {
  2013. status = -ENOMEM;
  2014. goto fail_tx;
  2015. }
  2016. /* return assigned endpoint to caller */
  2017. conn_resp->endpoint = assigned_ep;
  2018. conn_resp->len_max = max_msg_sz;
  2019. /* setup the endpoint */
  2020. /* this marks the endpoint in use */
  2021. endpoint->svc_id = conn_req->svc_id;
  2022. endpoint->max_txq_depth = conn_req->max_txq_depth;
  2023. endpoint->len_max = max_msg_sz;
  2024. endpoint->ep_cb = conn_req->ep_cb;
  2025. endpoint->cred_dist.svc_id = conn_req->svc_id;
  2026. endpoint->cred_dist.htc_ep = endpoint;
  2027. endpoint->cred_dist.endpoint = assigned_ep;
  2028. endpoint->cred_dist.cred_sz = target->tgt_cred_sz;
  2029. switch (endpoint->svc_id) {
  2030. case WMI_DATA_BK_SVC:
  2031. endpoint->tx_drop_packet_threshold = MAX_DEF_COOKIE_NUM / 3;
  2032. break;
  2033. default:
  2034. endpoint->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
  2035. break;
  2036. }
  2037. if (conn_req->max_rxmsg_sz) {
  2038. /*
  2039. * Override cred_per_msg calculation, this optimizes
  2040. * the credit-low indications since the host will actually
  2041. * issue smaller messages in the Send path.
  2042. */
  2043. if (conn_req->max_rxmsg_sz > max_msg_sz) {
  2044. status = -ENOMEM;
  2045. goto fail_tx;
  2046. }
  2047. endpoint->cred_dist.cred_per_msg =
  2048. conn_req->max_rxmsg_sz / target->tgt_cred_sz;
  2049. } else
  2050. endpoint->cred_dist.cred_per_msg =
  2051. max_msg_sz / target->tgt_cred_sz;
  2052. if (!endpoint->cred_dist.cred_per_msg)
  2053. endpoint->cred_dist.cred_per_msg = 1;
  2054. /* save local connection flags */
  2055. endpoint->conn_flags = conn_req->flags;
  2056. fail_tx:
  2057. if (tx_pkt)
  2058. htc_reclaim_txctrl_buf(target, tx_pkt);
  2059. if (rx_pkt) {
  2060. htc_rxpkt_reset(rx_pkt);
  2061. reclaim_rx_ctrl_buf(target, rx_pkt);
  2062. }
  2063. return status;
  2064. }
  2065. static void reset_ep_state(struct htc_target *target)
  2066. {
  2067. struct htc_endpoint *endpoint;
  2068. int i;
  2069. for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
  2070. endpoint = &target->endpoint[i];
  2071. memset(&endpoint->cred_dist, 0, sizeof(endpoint->cred_dist));
  2072. endpoint->svc_id = 0;
  2073. endpoint->len_max = 0;
  2074. endpoint->max_txq_depth = 0;
  2075. memset(&endpoint->ep_st, 0,
  2076. sizeof(endpoint->ep_st));
  2077. INIT_LIST_HEAD(&endpoint->rx_bufq);
  2078. INIT_LIST_HEAD(&endpoint->txq);
  2079. endpoint->target = target;
  2080. }
  2081. /* reset distribution list */
  2082. /* FIXME: free existing entries */
  2083. INIT_LIST_HEAD(&target->cred_dist_list);
  2084. }
  2085. int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
  2086. enum htc_endpoint_id endpoint)
  2087. {
  2088. int num;
  2089. spin_lock_bh(&target->rx_lock);
  2090. num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
  2091. spin_unlock_bh(&target->rx_lock);
  2092. return num;
  2093. }
  2094. static void htc_setup_msg_bndl(struct htc_target *target)
  2095. {
  2096. /* limit what HTC can handle */
  2097. target->msg_per_bndl_max = min(HTC_HOST_MAX_MSG_PER_BUNDLE,
  2098. target->msg_per_bndl_max);
  2099. if (ath6kl_hif_enable_scatter(target->dev->ar)) {
  2100. target->msg_per_bndl_max = 0;
  2101. return;
  2102. }
  2103. /* limit bundle what the device layer can handle */
  2104. target->msg_per_bndl_max = min(target->max_scat_entries,
  2105. target->msg_per_bndl_max);
  2106. ath6kl_dbg(ATH6KL_DBG_BOOT,
  2107. "htc bundling allowed msg_per_bndl_max %d\n",
  2108. target->msg_per_bndl_max);
  2109. /* Max rx bundle size is limited by the max tx bundle size */
  2110. target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
  2111. /* Max tx bundle size if limited by the extended mbox address range */
  2112. target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
  2113. target->max_xfer_szper_scatreq);
  2114. ath6kl_dbg(ATH6KL_DBG_BOOT, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
  2115. target->max_rx_bndl_sz, target->max_tx_bndl_sz);
  2116. if (target->max_tx_bndl_sz)
  2117. /* tx_bndl_mask is enabled per AC, each has 1 bit */
  2118. target->tx_bndl_mask = (1 << WMM_NUM_AC) - 1;
  2119. if (target->max_rx_bndl_sz)
  2120. target->rx_bndl_enable = true;
  2121. if ((target->tgt_cred_sz % target->block_sz) != 0) {
  2122. ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
  2123. target->tgt_cred_sz);
  2124. /*
  2125. * Disallow send bundling since the credit size is
  2126. * not aligned to a block size the I/O block
  2127. * padding will spill into the next credit buffer
  2128. * which is fatal.
  2129. */
  2130. target->tx_bndl_mask = 0;
  2131. }
  2132. }
  2133. int ath6kl_htc_wait_target(struct htc_target *target)
  2134. {
  2135. struct htc_packet *packet = NULL;
  2136. struct htc_ready_ext_msg *rdy_msg;
  2137. struct htc_service_connect_req connect;
  2138. struct htc_service_connect_resp resp;
  2139. int status;
  2140. /* FIXME: remove once USB support is implemented */
  2141. if (target->dev->ar->hif_type == ATH6KL_HIF_TYPE_USB) {
  2142. ath6kl_err("HTC doesn't support USB yet. Patience!\n");
  2143. return -EOPNOTSUPP;
  2144. }
  2145. /* we should be getting 1 control message that the target is ready */
  2146. packet = htc_wait_for_ctrl_msg(target);
  2147. if (!packet)
  2148. return -ENOMEM;
  2149. /* we controlled the buffer creation so it's properly aligned */
  2150. rdy_msg = (struct htc_ready_ext_msg *)packet->buf;
  2151. if ((le16_to_cpu(rdy_msg->ver2_0_info.msg_id) != HTC_MSG_READY_ID) ||
  2152. (packet->act_len < sizeof(struct htc_ready_msg))) {
  2153. status = -ENOMEM;
  2154. goto fail_wait_target;
  2155. }
  2156. if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
  2157. status = -ENOMEM;
  2158. goto fail_wait_target;
  2159. }
  2160. target->tgt_creds = le16_to_cpu(rdy_msg->ver2_0_info.cred_cnt);
  2161. target->tgt_cred_sz = le16_to_cpu(rdy_msg->ver2_0_info.cred_sz);
  2162. ath6kl_dbg(ATH6KL_DBG_BOOT,
  2163. "htc target ready credits %d size %d\n",
  2164. target->tgt_creds, target->tgt_cred_sz);
  2165. /* check if this is an extended ready message */
  2166. if (packet->act_len >= sizeof(struct htc_ready_ext_msg)) {
  2167. /* this is an extended message */
  2168. target->htc_tgt_ver = rdy_msg->htc_ver;
  2169. target->msg_per_bndl_max = rdy_msg->msg_per_htc_bndl;
  2170. } else {
  2171. /* legacy */
  2172. target->htc_tgt_ver = HTC_VERSION_2P0;
  2173. target->msg_per_bndl_max = 0;
  2174. }
  2175. ath6kl_dbg(ATH6KL_DBG_BOOT, "htc using protocol %s (%d)\n",
  2176. (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
  2177. target->htc_tgt_ver);
  2178. if (target->msg_per_bndl_max > 0)
  2179. htc_setup_msg_bndl(target);
  2180. /* setup our pseudo HTC control endpoint connection */
  2181. memset(&connect, 0, sizeof(connect));
  2182. memset(&resp, 0, sizeof(resp));
  2183. connect.ep_cb.rx = htc_ctrl_rx;
  2184. connect.ep_cb.rx_refill = NULL;
  2185. connect.ep_cb.tx_full = NULL;
  2186. connect.max_txq_depth = NUM_CONTROL_BUFFERS;
  2187. connect.svc_id = HTC_CTRL_RSVD_SVC;
  2188. /* connect fake service */
  2189. status = ath6kl_htc_conn_service((void *)target, &connect, &resp);
  2190. if (status)
  2191. /*
  2192. * FIXME: this call doesn't make sense, the caller should
  2193. * call ath6kl_htc_cleanup() when it wants remove htc
  2194. */
  2195. ath6kl_hif_cleanup_scatter(target->dev->ar);
  2196. fail_wait_target:
  2197. if (packet) {
  2198. htc_rxpkt_reset(packet);
  2199. reclaim_rx_ctrl_buf(target, packet);
  2200. }
  2201. return status;
  2202. }
  2203. /*
  2204. * Start HTC, enable interrupts and let the target know
  2205. * host has finished setup.
  2206. */
  2207. int ath6kl_htc_start(struct htc_target *target)
  2208. {
  2209. struct htc_packet *packet;
  2210. int status;
  2211. memset(&target->dev->irq_proc_reg, 0,
  2212. sizeof(target->dev->irq_proc_reg));
  2213. /* Disable interrupts at the chip level */
  2214. ath6kl_hif_disable_intrs(target->dev);
  2215. target->htc_flags = 0;
  2216. target->rx_st_flags = 0;
  2217. /* Push control receive buffers into htc control endpoint */
  2218. while ((packet = htc_get_control_buf(target, false)) != NULL) {
  2219. status = htc_add_rxbuf(target, packet);
  2220. if (status)
  2221. return status;
  2222. }
  2223. /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
  2224. ath6kl_credit_init(target->credit_info, &target->cred_dist_list,
  2225. target->tgt_creds);
  2226. dump_cred_dist_stats(target);
  2227. /* Indicate to the target of the setup completion */
  2228. status = htc_setup_tx_complete(target);
  2229. if (status)
  2230. return status;
  2231. /* unmask interrupts */
  2232. status = ath6kl_hif_unmask_intrs(target->dev);
  2233. if (status)
  2234. ath6kl_htc_stop(target);
  2235. return status;
  2236. }
  2237. static int ath6kl_htc_reset(struct htc_target *target)
  2238. {
  2239. u32 block_size, ctrl_bufsz;
  2240. struct htc_packet *packet;
  2241. int i;
  2242. reset_ep_state(target);
  2243. block_size = target->dev->ar->mbox_info.block_size;
  2244. ctrl_bufsz = (block_size > HTC_MAX_CTRL_MSG_LEN) ?
  2245. (block_size + HTC_HDR_LENGTH) :
  2246. (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH);
  2247. for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
  2248. packet = kzalloc(sizeof(*packet), GFP_KERNEL);
  2249. if (!packet)
  2250. return -ENOMEM;
  2251. packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
  2252. if (!packet->buf_start) {
  2253. kfree(packet);
  2254. return -ENOMEM;
  2255. }
  2256. packet->buf_len = ctrl_bufsz;
  2257. if (i < NUM_CONTROL_RX_BUFFERS) {
  2258. packet->act_len = 0;
  2259. packet->buf = packet->buf_start;
  2260. packet->endpoint = ENDPOINT_0;
  2261. list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
  2262. } else
  2263. list_add_tail(&packet->list, &target->free_ctrl_txbuf);
  2264. }
  2265. return 0;
  2266. }
  2267. /* htc_stop: stop interrupt reception, and flush all queued buffers */
  2268. void ath6kl_htc_stop(struct htc_target *target)
  2269. {
  2270. spin_lock_bh(&target->htc_lock);
  2271. target->htc_flags |= HTC_OP_STATE_STOPPING;
  2272. spin_unlock_bh(&target->htc_lock);
  2273. /*
  2274. * Masking interrupts is a synchronous operation, when this
  2275. * function returns all pending HIF I/O has completed, we can
  2276. * safely flush the queues.
  2277. */
  2278. ath6kl_hif_mask_intrs(target->dev);
  2279. ath6kl_htc_flush_txep_all(target);
  2280. ath6kl_htc_flush_rx_buf(target);
  2281. ath6kl_htc_reset(target);
  2282. }
  2283. void *ath6kl_htc_create(struct ath6kl *ar)
  2284. {
  2285. struct htc_target *target = NULL;
  2286. int status = 0;
  2287. target = kzalloc(sizeof(*target), GFP_KERNEL);
  2288. if (!target) {
  2289. ath6kl_err("unable to allocate memory\n");
  2290. return NULL;
  2291. }
  2292. target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
  2293. if (!target->dev) {
  2294. ath6kl_err("unable to allocate memory\n");
  2295. status = -ENOMEM;
  2296. goto err_htc_cleanup;
  2297. }
  2298. spin_lock_init(&target->htc_lock);
  2299. spin_lock_init(&target->rx_lock);
  2300. spin_lock_init(&target->tx_lock);
  2301. INIT_LIST_HEAD(&target->free_ctrl_txbuf);
  2302. INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
  2303. INIT_LIST_HEAD(&target->cred_dist_list);
  2304. target->dev->ar = ar;
  2305. target->dev->htc_cnxt = target;
  2306. target->ep_waiting = ENDPOINT_MAX;
  2307. status = ath6kl_hif_setup(target->dev);
  2308. if (status)
  2309. goto err_htc_cleanup;
  2310. status = ath6kl_htc_reset(target);
  2311. if (status)
  2312. goto err_htc_cleanup;
  2313. return target;
  2314. err_htc_cleanup:
  2315. ath6kl_htc_cleanup(target);
  2316. return NULL;
  2317. }
  2318. /* cleanup the HTC instance */
  2319. void ath6kl_htc_cleanup(struct htc_target *target)
  2320. {
  2321. struct htc_packet *packet, *tmp_packet;
  2322. /* FIXME: remove check once USB support is implemented */
  2323. if (target->dev->ar->hif_type != ATH6KL_HIF_TYPE_USB)
  2324. ath6kl_hif_cleanup_scatter(target->dev->ar);
  2325. list_for_each_entry_safe(packet, tmp_packet,
  2326. &target->free_ctrl_txbuf, list) {
  2327. list_del(&packet->list);
  2328. kfree(packet->buf_start);
  2329. kfree(packet);
  2330. }
  2331. list_for_each_entry_safe(packet, tmp_packet,
  2332. &target->free_ctrl_rxbuf, list) {
  2333. list_del(&packet->list);
  2334. kfree(packet->buf_start);
  2335. kfree(packet);
  2336. }
  2337. kfree(target->dev);
  2338. kfree(target);
  2339. }