htc.c 70 KB

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