htc.c 69 KB

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