htc_mbox.c 75 KB

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