htc.c 70 KB

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