htc_mbox.c 74 KB

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