htc.c 72 KB

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