htc.c 73 KB

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