htc.c 70 KB

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