htc_mbox.c 74 KB

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