htc_mbox.c 75 KB

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