hal_api.h 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760
  1. /*
  2. * Copyright (c) 2016-2021 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef _HAL_API_H_
  19. #define _HAL_API_H_
  20. #include "qdf_types.h"
  21. #include "qdf_util.h"
  22. #include "qdf_atomic.h"
  23. #include "hal_internal.h"
  24. #include "hif.h"
  25. #include "hif_io32.h"
  26. #include "qdf_platform.h"
  27. /* Ring index for WBM2SW2 release ring */
  28. #define HAL_IPA_TX_COMP_RING_IDX 2
  29. /* calculate the register address offset from bar0 of shadow register x */
  30. #if defined(QCA_WIFI_QCA6390) || defined(QCA_WIFI_QCA6490)
  31. #define SHADOW_REGISTER_START_ADDRESS_OFFSET 0x000008FC
  32. #define SHADOW_REGISTER_END_ADDRESS_OFFSET \
  33. ((SHADOW_REGISTER_START_ADDRESS_OFFSET) + (4 * (MAX_SHADOW_REGISTERS)))
  34. #define SHADOW_REGISTER(x) ((SHADOW_REGISTER_START_ADDRESS_OFFSET) + (4 * (x)))
  35. #elif defined(QCA_WIFI_QCA6290) || defined(QCA_WIFI_QCN9000)
  36. #define SHADOW_REGISTER_START_ADDRESS_OFFSET 0x00003024
  37. #define SHADOW_REGISTER_END_ADDRESS_OFFSET \
  38. ((SHADOW_REGISTER_START_ADDRESS_OFFSET) + (4 * (MAX_SHADOW_REGISTERS)))
  39. #define SHADOW_REGISTER(x) ((SHADOW_REGISTER_START_ADDRESS_OFFSET) + (4 * (x)))
  40. #elif defined(QCA_WIFI_QCA6750)
  41. #define SHADOW_REGISTER_START_ADDRESS_OFFSET 0x00000504
  42. #define SHADOW_REGISTER_END_ADDRESS_OFFSET \
  43. ((SHADOW_REGISTER_START_ADDRESS_OFFSET) + (4 * (MAX_SHADOW_REGISTERS)))
  44. #define SHADOW_REGISTER(x) ((SHADOW_REGISTER_START_ADDRESS_OFFSET) + (4 * (x)))
  45. #else
  46. #define SHADOW_REGISTER(x) 0
  47. #endif /* QCA_WIFI_QCA6390 || QCA_WIFI_QCA6490 || QCA_WIFI_QCA6750 */
  48. /*
  49. * BAR + 4K is always accessible, any access outside this
  50. * space requires force wake procedure.
  51. * OFFSET = 4K - 32 bytes = 0xFE0
  52. */
  53. #define MAPPED_REF_OFF 0xFE0
  54. #ifdef ENABLE_VERBOSE_DEBUG
  55. static inline void
  56. hal_set_verbose_debug(bool flag)
  57. {
  58. is_hal_verbose_debug_enabled = flag;
  59. }
  60. #endif
  61. #ifdef ENABLE_HAL_SOC_STATS
  62. #define HAL_STATS_INC(_handle, _field, _delta) \
  63. { \
  64. if (likely(_handle)) \
  65. _handle->stats._field += _delta; \
  66. }
  67. #else
  68. #define HAL_STATS_INC(_handle, _field, _delta)
  69. #endif
  70. #ifdef ENABLE_HAL_REG_WR_HISTORY
  71. #define HAL_REG_WRITE_FAIL_HIST_ADD(hal_soc, offset, wr_val, rd_val) \
  72. hal_reg_wr_fail_history_add(hal_soc, offset, wr_val, rd_val)
  73. void hal_reg_wr_fail_history_add(struct hal_soc *hal_soc,
  74. uint32_t offset,
  75. uint32_t wr_val,
  76. uint32_t rd_val);
  77. static inline int hal_history_get_next_index(qdf_atomic_t *table_index,
  78. int array_size)
  79. {
  80. int record_index = qdf_atomic_inc_return(table_index);
  81. return record_index & (array_size - 1);
  82. }
  83. #else
  84. #define HAL_REG_WRITE_FAIL_HIST_ADD(hal_soc, offset, wr_val, rd_val) \
  85. hal_err("write failed at reg offset 0x%x, write 0x%x read 0x%x\n", \
  86. offset, \
  87. wr_val, \
  88. rd_val)
  89. #endif
  90. /**
  91. * hal_reg_write_result_check() - check register writing result
  92. * @hal_soc: HAL soc handle
  93. * @offset: register offset to read
  94. * @exp_val: the expected value of register
  95. * @ret_confirm: result confirm flag
  96. *
  97. * Return: none
  98. */
  99. static inline void hal_reg_write_result_check(struct hal_soc *hal_soc,
  100. uint32_t offset,
  101. uint32_t exp_val)
  102. {
  103. uint32_t value;
  104. value = qdf_ioread32(hal_soc->dev_base_addr + offset);
  105. if (exp_val != value) {
  106. HAL_REG_WRITE_FAIL_HIST_ADD(hal_soc, offset, exp_val, value);
  107. HAL_STATS_INC(hal_soc, reg_write_fail, 1);
  108. }
  109. }
  110. #if !defined(QCA_WIFI_QCA6390) && !defined(QCA_WIFI_QCA6490)
  111. static inline void hal_lock_reg_access(struct hal_soc *soc,
  112. unsigned long *flags)
  113. {
  114. qdf_spin_lock_irqsave(&soc->register_access_lock);
  115. }
  116. static inline void hal_unlock_reg_access(struct hal_soc *soc,
  117. unsigned long *flags)
  118. {
  119. qdf_spin_unlock_irqrestore(&soc->register_access_lock);
  120. }
  121. #else
  122. static inline void hal_lock_reg_access(struct hal_soc *soc,
  123. unsigned long *flags)
  124. {
  125. pld_lock_reg_window(soc->qdf_dev->dev, flags);
  126. }
  127. static inline void hal_unlock_reg_access(struct hal_soc *soc,
  128. unsigned long *flags)
  129. {
  130. pld_unlock_reg_window(soc->qdf_dev->dev, flags);
  131. }
  132. #endif
  133. #ifdef PCIE_REG_WINDOW_LOCAL_NO_CACHE
  134. /**
  135. * hal_select_window_confirm() - write remap window register and
  136. check writing result
  137. *
  138. */
  139. static inline void hal_select_window_confirm(struct hal_soc *hal_soc,
  140. uint32_t offset)
  141. {
  142. uint32_t window = (offset >> WINDOW_SHIFT) & WINDOW_VALUE_MASK;
  143. qdf_iowrite32(hal_soc->dev_base_addr + WINDOW_REG_ADDRESS,
  144. WINDOW_ENABLE_BIT | window);
  145. hal_soc->register_window = window;
  146. hal_reg_write_result_check(hal_soc, WINDOW_REG_ADDRESS,
  147. WINDOW_ENABLE_BIT | window);
  148. }
  149. #else
  150. static inline void hal_select_window_confirm(struct hal_soc *hal_soc,
  151. uint32_t offset)
  152. {
  153. uint32_t window = (offset >> WINDOW_SHIFT) & WINDOW_VALUE_MASK;
  154. if (window != hal_soc->register_window) {
  155. qdf_iowrite32(hal_soc->dev_base_addr + WINDOW_REG_ADDRESS,
  156. WINDOW_ENABLE_BIT | window);
  157. hal_soc->register_window = window;
  158. hal_reg_write_result_check(
  159. hal_soc,
  160. WINDOW_REG_ADDRESS,
  161. WINDOW_ENABLE_BIT | window);
  162. }
  163. }
  164. #endif
  165. static inline qdf_iomem_t hal_get_window_address(struct hal_soc *hal_soc,
  166. qdf_iomem_t addr)
  167. {
  168. return hal_soc->ops->hal_get_window_address(hal_soc, addr);
  169. }
  170. static inline void hal_tx_init_cmd_credit_ring(hal_soc_handle_t hal_soc_hdl,
  171. hal_ring_handle_t hal_ring_hdl)
  172. {
  173. struct hal_soc *hal_soc = (struct hal_soc *)hal_soc_hdl;
  174. return hal_soc->ops->hal_tx_init_cmd_credit_ring(hal_soc_hdl,
  175. hal_ring_hdl);
  176. }
  177. /**
  178. * hal_write32_mb() - Access registers to update configuration
  179. * @hal_soc: hal soc handle
  180. * @offset: offset address from the BAR
  181. * @value: value to write
  182. *
  183. * Return: None
  184. *
  185. * Description: Register address space is split below:
  186. * SHADOW REGION UNWINDOWED REGION WINDOWED REGION
  187. * |--------------------|-------------------|------------------|
  188. * BAR NO FORCE WAKE BAR+4K FORCE WAKE BAR+512K FORCE WAKE
  189. *
  190. * 1. Any access to the shadow region, doesn't need force wake
  191. * and windowing logic to access.
  192. * 2. Any access beyond BAR + 4K:
  193. * If init_phase enabled, no force wake is needed and access
  194. * should be based on windowed or unwindowed access.
  195. * If init_phase disabled, force wake is needed and access
  196. * should be based on windowed or unwindowed access.
  197. *
  198. * note1: WINDOW_RANGE_MASK = (1 << WINDOW_SHIFT) -1
  199. * note2: 1 << WINDOW_SHIFT = MAX_UNWINDOWED_ADDRESS
  200. * note3: WINDOW_VALUE_MASK = big enough that trying to write past
  201. * that window would be a bug
  202. */
  203. #if !defined(QCA_WIFI_QCA6390) && !defined(QCA_WIFI_QCA6490) && \
  204. !defined(QCA_WIFI_QCA6750)
  205. static inline void hal_write32_mb(struct hal_soc *hal_soc, uint32_t offset,
  206. uint32_t value)
  207. {
  208. unsigned long flags;
  209. qdf_iomem_t new_addr;
  210. if (!hal_soc->use_register_windowing ||
  211. offset < MAX_UNWINDOWED_ADDRESS) {
  212. qdf_iowrite32(hal_soc->dev_base_addr + offset, value);
  213. } else if (hal_soc->static_window_map) {
  214. new_addr = hal_get_window_address(hal_soc,
  215. hal_soc->dev_base_addr + offset);
  216. qdf_iowrite32(new_addr, value);
  217. } else {
  218. hal_lock_reg_access(hal_soc, &flags);
  219. hal_select_window_confirm(hal_soc, offset);
  220. qdf_iowrite32(hal_soc->dev_base_addr + WINDOW_START +
  221. (offset & WINDOW_RANGE_MASK), value);
  222. hal_unlock_reg_access(hal_soc, &flags);
  223. }
  224. }
  225. #define hal_write32_mb_confirm(_hal_soc, _offset, _value) \
  226. hal_write32_mb(_hal_soc, _offset, _value)
  227. #define hal_write32_mb_cmem(_hal_soc, _offset, _value)
  228. #else
  229. static inline void hal_write32_mb(struct hal_soc *hal_soc, uint32_t offset,
  230. uint32_t value)
  231. {
  232. int ret;
  233. unsigned long flags;
  234. qdf_iomem_t new_addr;
  235. if (!TARGET_ACCESS_ALLOWED(HIF_GET_SOFTC(
  236. hal_soc->hif_handle))) {
  237. hal_err_rl("target access is not allowed");
  238. return;
  239. }
  240. /* Region < BAR + 4K can be directly accessed */
  241. if (offset < MAPPED_REF_OFF) {
  242. qdf_iowrite32(hal_soc->dev_base_addr + offset, value);
  243. return;
  244. }
  245. /* Region greater than BAR + 4K */
  246. if (!hal_soc->init_phase) {
  247. ret = hif_force_wake_request(hal_soc->hif_handle);
  248. if (ret) {
  249. hal_err_rl("Wake up request failed");
  250. qdf_check_state_before_panic(__func__, __LINE__);
  251. return;
  252. }
  253. }
  254. if (!hal_soc->use_register_windowing ||
  255. offset < MAX_UNWINDOWED_ADDRESS) {
  256. qdf_iowrite32(hal_soc->dev_base_addr + offset, value);
  257. } else if (hal_soc->static_window_map) {
  258. new_addr = hal_get_window_address(
  259. hal_soc,
  260. hal_soc->dev_base_addr + offset);
  261. qdf_iowrite32(new_addr, value);
  262. } else {
  263. hal_lock_reg_access(hal_soc, &flags);
  264. hal_select_window_confirm(hal_soc, offset);
  265. qdf_iowrite32(hal_soc->dev_base_addr + WINDOW_START +
  266. (offset & WINDOW_RANGE_MASK), value);
  267. hal_unlock_reg_access(hal_soc, &flags);
  268. }
  269. if (!hal_soc->init_phase) {
  270. ret = hif_force_wake_release(hal_soc->hif_handle);
  271. if (ret) {
  272. hal_err("Wake up release failed");
  273. qdf_check_state_before_panic(__func__, __LINE__);
  274. return;
  275. }
  276. }
  277. }
  278. /**
  279. * hal_write32_mb_confirm() - write register and check wirting result
  280. *
  281. */
  282. static inline void hal_write32_mb_confirm(struct hal_soc *hal_soc,
  283. uint32_t offset,
  284. uint32_t value)
  285. {
  286. int ret;
  287. unsigned long flags;
  288. qdf_iomem_t new_addr;
  289. if (!TARGET_ACCESS_ALLOWED(HIF_GET_SOFTC(
  290. hal_soc->hif_handle))) {
  291. hal_err_rl("target access is not allowed");
  292. return;
  293. }
  294. /* Region < BAR + 4K can be directly accessed */
  295. if (offset < MAPPED_REF_OFF) {
  296. qdf_iowrite32(hal_soc->dev_base_addr + offset, value);
  297. return;
  298. }
  299. /* Region greater than BAR + 4K */
  300. if (!hal_soc->init_phase) {
  301. ret = hif_force_wake_request(hal_soc->hif_handle);
  302. if (ret) {
  303. hal_err("Wake up request failed");
  304. qdf_check_state_before_panic(__func__, __LINE__);
  305. return;
  306. }
  307. }
  308. if (!hal_soc->use_register_windowing ||
  309. offset < MAX_UNWINDOWED_ADDRESS) {
  310. qdf_iowrite32(hal_soc->dev_base_addr + offset, value);
  311. hal_reg_write_result_check(hal_soc, offset,
  312. value);
  313. } else if (hal_soc->static_window_map) {
  314. new_addr = hal_get_window_address(
  315. hal_soc,
  316. hal_soc->dev_base_addr + offset);
  317. qdf_iowrite32(new_addr, value);
  318. hal_reg_write_result_check(hal_soc,
  319. new_addr - hal_soc->dev_base_addr,
  320. value);
  321. } else {
  322. hal_lock_reg_access(hal_soc, &flags);
  323. hal_select_window_confirm(hal_soc, offset);
  324. qdf_iowrite32(hal_soc->dev_base_addr + WINDOW_START +
  325. (offset & WINDOW_RANGE_MASK), value);
  326. hal_reg_write_result_check(
  327. hal_soc,
  328. WINDOW_START + (offset & WINDOW_RANGE_MASK),
  329. value);
  330. hal_unlock_reg_access(hal_soc, &flags);
  331. }
  332. if (!hal_soc->init_phase) {
  333. ret = hif_force_wake_release(hal_soc->hif_handle);
  334. if (ret) {
  335. hal_err("Wake up release failed");
  336. qdf_check_state_before_panic(__func__, __LINE__);
  337. return;
  338. }
  339. }
  340. }
  341. static inline void hal_write32_mb_cmem(struct hal_soc *hal_soc, uint32_t offset,
  342. uint32_t value)
  343. {
  344. unsigned long flags;
  345. qdf_iomem_t new_addr;
  346. if (!TARGET_ACCESS_ALLOWED(HIF_GET_SOFTC(
  347. hal_soc->hif_handle))) {
  348. hal_err_rl("%s: target access is not allowed", __func__);
  349. return;
  350. }
  351. if (!hal_soc->use_register_windowing ||
  352. offset < MAX_UNWINDOWED_ADDRESS) {
  353. qdf_iowrite32(hal_soc->dev_base_addr + offset, value);
  354. } else if (hal_soc->static_window_map) {
  355. new_addr = hal_get_window_address(
  356. hal_soc,
  357. hal_soc->dev_base_addr + offset);
  358. qdf_iowrite32(new_addr, value);
  359. } else {
  360. hal_lock_reg_access(hal_soc, &flags);
  361. hal_select_window_confirm(hal_soc, offset);
  362. qdf_iowrite32(hal_soc->dev_base_addr + WINDOW_START +
  363. (offset & WINDOW_RANGE_MASK), value);
  364. hal_unlock_reg_access(hal_soc, &flags);
  365. }
  366. }
  367. #endif
  368. /**
  369. * hal_write_address_32_mb - write a value to a register
  370. *
  371. */
  372. static inline
  373. void hal_write_address_32_mb(struct hal_soc *hal_soc,
  374. qdf_iomem_t addr, uint32_t value, bool wr_confirm)
  375. {
  376. uint32_t offset;
  377. if (!hal_soc->use_register_windowing)
  378. return qdf_iowrite32(addr, value);
  379. offset = addr - hal_soc->dev_base_addr;
  380. if (qdf_unlikely(wr_confirm))
  381. hal_write32_mb_confirm(hal_soc, offset, value);
  382. else
  383. hal_write32_mb(hal_soc, offset, value);
  384. }
  385. #ifdef DP_HAL_MULTIWINDOW_DIRECT_ACCESS
  386. static inline void hal_srng_write_address_32_mb(struct hal_soc *hal_soc,
  387. struct hal_srng *srng,
  388. void __iomem *addr,
  389. uint32_t value)
  390. {
  391. qdf_iowrite32(addr, value);
  392. }
  393. #elif defined(FEATURE_HAL_DELAYED_REG_WRITE) || \
  394. defined(FEATURE_HAL_DELAYED_REG_WRITE_V2)
  395. static inline void hal_srng_write_address_32_mb(struct hal_soc *hal_soc,
  396. struct hal_srng *srng,
  397. void __iomem *addr,
  398. uint32_t value)
  399. {
  400. hal_delayed_reg_write(hal_soc, srng, addr, value);
  401. }
  402. #else
  403. static inline void hal_srng_write_address_32_mb(struct hal_soc *hal_soc,
  404. struct hal_srng *srng,
  405. void __iomem *addr,
  406. uint32_t value)
  407. {
  408. hal_write_address_32_mb(hal_soc, addr, value, false);
  409. }
  410. #endif
  411. #if !defined(QCA_WIFI_QCA6390) && !defined(QCA_WIFI_QCA6490) && \
  412. !defined(QCA_WIFI_QCA6750)
  413. /**
  414. * hal_read32_mb() - Access registers to read configuration
  415. * @hal_soc: hal soc handle
  416. * @offset: offset address from the BAR
  417. * @value: value to write
  418. *
  419. * Description: Register address space is split below:
  420. * SHADOW REGION UNWINDOWED REGION WINDOWED REGION
  421. * |--------------------|-------------------|------------------|
  422. * BAR NO FORCE WAKE BAR+4K FORCE WAKE BAR+512K FORCE WAKE
  423. *
  424. * 1. Any access to the shadow region, doesn't need force wake
  425. * and windowing logic to access.
  426. * 2. Any access beyond BAR + 4K:
  427. * If init_phase enabled, no force wake is needed and access
  428. * should be based on windowed or unwindowed access.
  429. * If init_phase disabled, force wake is needed and access
  430. * should be based on windowed or unwindowed access.
  431. *
  432. * Return: < 0 for failure/>= 0 for success
  433. */
  434. static inline uint32_t hal_read32_mb(struct hal_soc *hal_soc, uint32_t offset)
  435. {
  436. uint32_t ret;
  437. unsigned long flags;
  438. qdf_iomem_t new_addr;
  439. if (!hal_soc->use_register_windowing ||
  440. offset < MAX_UNWINDOWED_ADDRESS) {
  441. return qdf_ioread32(hal_soc->dev_base_addr + offset);
  442. } else if (hal_soc->static_window_map) {
  443. new_addr = hal_get_window_address(hal_soc, hal_soc->dev_base_addr + offset);
  444. return qdf_ioread32(new_addr);
  445. }
  446. hal_lock_reg_access(hal_soc, &flags);
  447. hal_select_window_confirm(hal_soc, offset);
  448. ret = qdf_ioread32(hal_soc->dev_base_addr + WINDOW_START +
  449. (offset & WINDOW_RANGE_MASK));
  450. hal_unlock_reg_access(hal_soc, &flags);
  451. return ret;
  452. }
  453. #define hal_read32_mb_cmem(_hal_soc, _offset)
  454. #else
  455. static
  456. uint32_t hal_read32_mb(struct hal_soc *hal_soc, uint32_t offset)
  457. {
  458. uint32_t ret;
  459. unsigned long flags;
  460. qdf_iomem_t new_addr;
  461. if (!TARGET_ACCESS_ALLOWED(HIF_GET_SOFTC(
  462. hal_soc->hif_handle))) {
  463. hal_err_rl("target access is not allowed");
  464. return 0;
  465. }
  466. /* Region < BAR + 4K can be directly accessed */
  467. if (offset < MAPPED_REF_OFF)
  468. return qdf_ioread32(hal_soc->dev_base_addr + offset);
  469. if ((!hal_soc->init_phase) &&
  470. hif_force_wake_request(hal_soc->hif_handle)) {
  471. hal_err("Wake up request failed");
  472. qdf_check_state_before_panic(__func__, __LINE__);
  473. return 0;
  474. }
  475. if (!hal_soc->use_register_windowing ||
  476. offset < MAX_UNWINDOWED_ADDRESS) {
  477. ret = qdf_ioread32(hal_soc->dev_base_addr + offset);
  478. } else if (hal_soc->static_window_map) {
  479. new_addr = hal_get_window_address(
  480. hal_soc,
  481. hal_soc->dev_base_addr + offset);
  482. ret = qdf_ioread32(new_addr);
  483. } else {
  484. hal_lock_reg_access(hal_soc, &flags);
  485. hal_select_window_confirm(hal_soc, offset);
  486. ret = qdf_ioread32(hal_soc->dev_base_addr + WINDOW_START +
  487. (offset & WINDOW_RANGE_MASK));
  488. hal_unlock_reg_access(hal_soc, &flags);
  489. }
  490. if ((!hal_soc->init_phase) &&
  491. hif_force_wake_release(hal_soc->hif_handle)) {
  492. hal_err("Wake up release failed");
  493. qdf_check_state_before_panic(__func__, __LINE__);
  494. return 0;
  495. }
  496. return ret;
  497. }
  498. static inline
  499. uint32_t hal_read32_mb_cmem(struct hal_soc *hal_soc, uint32_t offset)
  500. {
  501. uint32_t ret;
  502. unsigned long flags;
  503. qdf_iomem_t new_addr;
  504. if (!TARGET_ACCESS_ALLOWED(HIF_GET_SOFTC(
  505. hal_soc->hif_handle))) {
  506. hal_err_rl("%s: target access is not allowed", __func__);
  507. return 0;
  508. }
  509. if (!hal_soc->use_register_windowing ||
  510. offset < MAX_UNWINDOWED_ADDRESS) {
  511. ret = qdf_ioread32(hal_soc->dev_base_addr + offset);
  512. } else if (hal_soc->static_window_map) {
  513. new_addr = hal_get_window_address(
  514. hal_soc,
  515. hal_soc->dev_base_addr + offset);
  516. ret = qdf_ioread32(new_addr);
  517. } else {
  518. hal_lock_reg_access(hal_soc, &flags);
  519. hal_select_window_confirm(hal_soc, offset);
  520. ret = qdf_ioread32(hal_soc->dev_base_addr + WINDOW_START +
  521. (offset & WINDOW_RANGE_MASK));
  522. hal_unlock_reg_access(hal_soc, &flags);
  523. }
  524. return ret;
  525. }
  526. #endif
  527. /* Max times allowed for register writing retry */
  528. #define HAL_REG_WRITE_RETRY_MAX 5
  529. /* Delay milliseconds for each time retry */
  530. #define HAL_REG_WRITE_RETRY_DELAY 1
  531. #ifdef GENERIC_SHADOW_REGISTER_ACCESS_ENABLE
  532. /* To check shadow config index range between 0..31 */
  533. #define HAL_SHADOW_REG_INDEX_LOW 32
  534. /* To check shadow config index range between 32..39 */
  535. #define HAL_SHADOW_REG_INDEX_HIGH 40
  536. /* Dirty bit reg offsets corresponding to shadow config index */
  537. #define HAL_SHADOW_REG_DIRTY_BIT_DATA_LOW_OFFSET 0x30C8
  538. #define HAL_SHADOW_REG_DIRTY_BIT_DATA_HIGH_OFFSET 0x30C4
  539. /* PCIE_PCIE_TOP base addr offset */
  540. #define HAL_PCIE_PCIE_TOP_WRAPPER 0x01E00000
  541. /* Max retry attempts to read the dirty bit reg */
  542. #ifdef HAL_CONFIG_SLUB_DEBUG_ON
  543. #define HAL_SHADOW_DIRTY_BIT_POLL_MAX 10000
  544. #else
  545. #define HAL_SHADOW_DIRTY_BIT_POLL_MAX 2000
  546. #endif
  547. /* Delay in usecs for polling dirty bit reg */
  548. #define HAL_SHADOW_DIRTY_BIT_POLL_DELAY 5
  549. /**
  550. * hal_poll_dirty_bit_reg() - Poll dirty register bit to confirm
  551. * write was successful
  552. * @hal_soc: hal soc handle
  553. * @shadow_config_index: index of shadow reg used to confirm
  554. * write
  555. *
  556. * Return: QDF_STATUS_SUCCESS on success
  557. */
  558. static inline QDF_STATUS hal_poll_dirty_bit_reg(struct hal_soc *hal,
  559. int shadow_config_index)
  560. {
  561. uint32_t read_value = 0;
  562. int retry_cnt = 0;
  563. uint32_t reg_offset = 0;
  564. if (shadow_config_index > 0 &&
  565. shadow_config_index < HAL_SHADOW_REG_INDEX_LOW) {
  566. reg_offset =
  567. HAL_SHADOW_REG_DIRTY_BIT_DATA_LOW_OFFSET;
  568. } else if (shadow_config_index >= HAL_SHADOW_REG_INDEX_LOW &&
  569. shadow_config_index < HAL_SHADOW_REG_INDEX_HIGH) {
  570. reg_offset =
  571. HAL_SHADOW_REG_DIRTY_BIT_DATA_HIGH_OFFSET;
  572. } else {
  573. hal_err("Invalid shadow_config_index = %d",
  574. shadow_config_index);
  575. return QDF_STATUS_E_INVAL;
  576. }
  577. while (retry_cnt < HAL_SHADOW_DIRTY_BIT_POLL_MAX) {
  578. read_value = hal_read32_mb(
  579. hal, HAL_PCIE_PCIE_TOP_WRAPPER + reg_offset);
  580. /* Check if dirty bit corresponding to shadow_index is set */
  581. if (read_value & BIT(shadow_config_index)) {
  582. /* Dirty reg bit not reset */
  583. qdf_udelay(HAL_SHADOW_DIRTY_BIT_POLL_DELAY);
  584. retry_cnt++;
  585. } else {
  586. hal_debug("Shadow write: offset 0x%x read val 0x%x",
  587. reg_offset, read_value);
  588. return QDF_STATUS_SUCCESS;
  589. }
  590. }
  591. return QDF_STATUS_E_TIMEOUT;
  592. }
  593. /**
  594. * hal_write32_mb_shadow_confirm() - write to shadow reg and
  595. * poll dirty register bit to confirm write
  596. * @hal_soc: hal soc handle
  597. * @reg_offset: target reg offset address from BAR
  598. * @value: value to write
  599. *
  600. * Return: QDF_STATUS_SUCCESS on success
  601. */
  602. static inline QDF_STATUS hal_write32_mb_shadow_confirm(
  603. struct hal_soc *hal,
  604. uint32_t reg_offset,
  605. uint32_t value)
  606. {
  607. int i;
  608. QDF_STATUS ret;
  609. uint32_t shadow_reg_offset;
  610. int shadow_config_index;
  611. bool is_reg_offset_present = false;
  612. for (i = 0; i < MAX_GENERIC_SHADOW_REG; i++) {
  613. /* Found the shadow config for the reg_offset */
  614. struct shadow_reg_config *hal_shadow_reg_list =
  615. &hal->list_shadow_reg_config[i];
  616. if (hal_shadow_reg_list->target_register ==
  617. reg_offset) {
  618. shadow_config_index =
  619. hal_shadow_reg_list->shadow_config_index;
  620. shadow_reg_offset =
  621. SHADOW_REGISTER(shadow_config_index);
  622. hal_write32_mb_confirm(
  623. hal, shadow_reg_offset, value);
  624. is_reg_offset_present = true;
  625. break;
  626. }
  627. ret = QDF_STATUS_E_FAILURE;
  628. }
  629. if (is_reg_offset_present) {
  630. ret = hal_poll_dirty_bit_reg(hal, shadow_config_index);
  631. hal_info("Shadow write:reg 0x%x val 0x%x ret %d",
  632. reg_offset, value, ret);
  633. if (QDF_IS_STATUS_ERROR(ret)) {
  634. HAL_STATS_INC(hal, shadow_reg_write_fail, 1);
  635. return ret;
  636. }
  637. HAL_STATS_INC(hal, shadow_reg_write_succ, 1);
  638. }
  639. return ret;
  640. }
  641. /**
  642. * hal_write32_mb_confirm_retry() - write register with confirming and
  643. do retry/recovery if writing failed
  644. * @hal_soc: hal soc handle
  645. * @offset: offset address from the BAR
  646. * @value: value to write
  647. * @recovery: is recovery needed or not.
  648. *
  649. * Write the register value with confirming and read it back, if
  650. * read back value is not as expected, do retry for writing, if
  651. * retry hit max times allowed but still fail, check if recovery
  652. * needed.
  653. *
  654. * Return: None
  655. */
  656. static inline void hal_write32_mb_confirm_retry(struct hal_soc *hal_soc,
  657. uint32_t offset,
  658. uint32_t value,
  659. bool recovery)
  660. {
  661. QDF_STATUS ret;
  662. ret = hal_write32_mb_shadow_confirm(hal_soc, offset, value);
  663. if (QDF_IS_STATUS_ERROR(ret) && recovery)
  664. qdf_trigger_self_recovery(NULL, QDF_HAL_REG_WRITE_FAILURE);
  665. }
  666. #else /* GENERIC_SHADOW_REGISTER_ACCESS_ENABLE */
  667. static inline void hal_write32_mb_confirm_retry(struct hal_soc *hal_soc,
  668. uint32_t offset,
  669. uint32_t value,
  670. bool recovery)
  671. {
  672. uint8_t retry_cnt = 0;
  673. uint32_t read_value;
  674. while (retry_cnt <= HAL_REG_WRITE_RETRY_MAX) {
  675. hal_write32_mb_confirm(hal_soc, offset, value);
  676. read_value = hal_read32_mb(hal_soc, offset);
  677. if (qdf_likely(read_value == value))
  678. break;
  679. /* write failed, do retry */
  680. hal_warn("Retry reg offset 0x%x, value 0x%x, read value 0x%x",
  681. offset, value, read_value);
  682. qdf_mdelay(HAL_REG_WRITE_RETRY_DELAY);
  683. retry_cnt++;
  684. }
  685. if (retry_cnt > HAL_REG_WRITE_RETRY_MAX && recovery)
  686. qdf_trigger_self_recovery(NULL, QDF_HAL_REG_WRITE_FAILURE);
  687. }
  688. #endif /* GENERIC_SHADOW_REGISTER_ACCESS_ENABLE */
  689. #if defined(FEATURE_HAL_DELAYED_REG_WRITE) || \
  690. defined(FEATURE_HAL_DELAYED_REG_WRITE_V2)
  691. /**
  692. * hal_dump_reg_write_srng_stats() - dump SRNG reg write stats
  693. * @hal_soc: HAL soc handle
  694. *
  695. * Return: none
  696. */
  697. void hal_dump_reg_write_srng_stats(hal_soc_handle_t hal_soc_hdl);
  698. /**
  699. * hal_dump_reg_write_stats() - dump reg write stats
  700. * @hal_soc: HAL soc handle
  701. *
  702. * Return: none
  703. */
  704. void hal_dump_reg_write_stats(hal_soc_handle_t hal_soc_hdl);
  705. /**
  706. * hal_get_reg_write_pending_work() - get the number of entries
  707. * pending in the workqueue to be processed.
  708. * @hal_soc: HAL soc handle
  709. *
  710. * Returns: the number of entries pending to be processed
  711. */
  712. int hal_get_reg_write_pending_work(void *hal_soc);
  713. #else
  714. static inline void hal_dump_reg_write_srng_stats(hal_soc_handle_t hal_soc_hdl)
  715. {
  716. }
  717. static inline void hal_dump_reg_write_stats(hal_soc_handle_t hal_soc_hdl)
  718. {
  719. }
  720. static inline int hal_get_reg_write_pending_work(void *hal_soc)
  721. {
  722. return 0;
  723. }
  724. #endif
  725. /**
  726. * hal_read_address_32_mb() - Read 32-bit value from the register
  727. * @soc: soc handle
  728. * @addr: register address to read
  729. *
  730. * Return: 32-bit value
  731. */
  732. static inline
  733. uint32_t hal_read_address_32_mb(struct hal_soc *soc,
  734. qdf_iomem_t addr)
  735. {
  736. uint32_t offset;
  737. uint32_t ret;
  738. if (!soc->use_register_windowing)
  739. return qdf_ioread32(addr);
  740. offset = addr - soc->dev_base_addr;
  741. ret = hal_read32_mb(soc, offset);
  742. return ret;
  743. }
  744. /**
  745. * hal_attach - Initialize HAL layer
  746. * @hif_handle: Opaque HIF handle
  747. * @qdf_dev: QDF device
  748. *
  749. * Return: Opaque HAL SOC handle
  750. * NULL on failure (if given ring is not available)
  751. *
  752. * This function should be called as part of HIF initialization (for accessing
  753. * copy engines). DP layer will get hal_soc handle using hif_get_hal_handle()
  754. */
  755. void *hal_attach(struct hif_opaque_softc *hif_handle, qdf_device_t qdf_dev);
  756. /**
  757. * hal_detach - Detach HAL layer
  758. * @hal_soc: HAL SOC handle
  759. *
  760. * This function should be called as part of HIF detach
  761. *
  762. */
  763. extern void hal_detach(void *hal_soc);
  764. #define HAL_SRNG_LMAC_RING 0x80000000
  765. /* SRNG flags passed in hal_srng_params.flags */
  766. #define HAL_SRNG_MSI_SWAP 0x00000008
  767. #define HAL_SRNG_RING_PTR_SWAP 0x00000010
  768. #define HAL_SRNG_DATA_TLV_SWAP 0x00000020
  769. #define HAL_SRNG_LOW_THRES_INTR_ENABLE 0x00010000
  770. #define HAL_SRNG_MSI_INTR 0x00020000
  771. #define HAL_SRNG_CACHED_DESC 0x00040000
  772. #ifdef QCA_WIFI_QCA6490
  773. #define HAL_SRNG_PREFETCH_TIMER 1
  774. #else
  775. #define HAL_SRNG_PREFETCH_TIMER 0
  776. #endif
  777. #define PN_SIZE_24 0
  778. #define PN_SIZE_48 1
  779. #define PN_SIZE_128 2
  780. #ifdef FORCE_WAKE
  781. /**
  782. * hal_set_init_phase() - Indicate initialization of
  783. * datapath rings
  784. * @soc: hal_soc handle
  785. * @init_phase: flag to indicate datapath rings
  786. * initialization status
  787. *
  788. * Return: None
  789. */
  790. void hal_set_init_phase(hal_soc_handle_t soc, bool init_phase);
  791. #else
  792. static inline
  793. void hal_set_init_phase(hal_soc_handle_t soc, bool init_phase)
  794. {
  795. }
  796. #endif /* FORCE_WAKE */
  797. /**
  798. * hal_srng_get_entrysize - Returns size of ring entry in bytes. Should be
  799. * used by callers for calculating the size of memory to be allocated before
  800. * calling hal_srng_setup to setup the ring
  801. *
  802. * @hal_soc: Opaque HAL SOC handle
  803. * @ring_type: one of the types from hal_ring_type
  804. *
  805. */
  806. extern uint32_t hal_srng_get_entrysize(void *hal_soc, int ring_type);
  807. /**
  808. * hal_srng_max_entries - Returns maximum possible number of ring entries
  809. * @hal_soc: Opaque HAL SOC handle
  810. * @ring_type: one of the types from hal_ring_type
  811. *
  812. * Return: Maximum number of entries for the given ring_type
  813. */
  814. uint32_t hal_srng_max_entries(void *hal_soc, int ring_type);
  815. void hal_set_low_threshold(hal_ring_handle_t hal_ring_hdl,
  816. uint32_t low_threshold);
  817. /**
  818. * hal_srng_dump - Dump ring status
  819. * @srng: hal srng pointer
  820. */
  821. void hal_srng_dump(struct hal_srng *srng);
  822. /**
  823. * hal_srng_get_dir - Returns the direction of the ring
  824. * @hal_soc: Opaque HAL SOC handle
  825. * @ring_type: one of the types from hal_ring_type
  826. *
  827. * Return: Ring direction
  828. */
  829. enum hal_srng_dir hal_srng_get_dir(void *hal_soc, int ring_type);
  830. /* HAL memory information */
  831. struct hal_mem_info {
  832. /* dev base virutal addr */
  833. void *dev_base_addr;
  834. /* dev base physical addr */
  835. void *dev_base_paddr;
  836. /* dev base ce virutal addr - applicable only for qca5018 */
  837. /* In qca5018 CE register are outside wcss block */
  838. /* using a separate address space to access CE registers */
  839. void *dev_base_addr_ce;
  840. /* dev base ce physical addr */
  841. void *dev_base_paddr_ce;
  842. /* Remote virtual pointer memory for HW/FW updates */
  843. void *shadow_rdptr_mem_vaddr;
  844. /* Remote physical pointer memory for HW/FW updates */
  845. void *shadow_rdptr_mem_paddr;
  846. /* Shared memory for ring pointer updates from host to FW */
  847. void *shadow_wrptr_mem_vaddr;
  848. /* Shared physical memory for ring pointer updates from host to FW */
  849. void *shadow_wrptr_mem_paddr;
  850. };
  851. /* SRNG parameters to be passed to hal_srng_setup */
  852. struct hal_srng_params {
  853. /* Physical base address of the ring */
  854. qdf_dma_addr_t ring_base_paddr;
  855. /* Virtual base address of the ring */
  856. void *ring_base_vaddr;
  857. /* Number of entries in ring */
  858. uint32_t num_entries;
  859. /* max transfer length */
  860. uint16_t max_buffer_length;
  861. /* MSI Address */
  862. qdf_dma_addr_t msi_addr;
  863. /* MSI data */
  864. uint32_t msi_data;
  865. /* Interrupt timer threshold – in micro seconds */
  866. uint32_t intr_timer_thres_us;
  867. /* Interrupt batch counter threshold – in number of ring entries */
  868. uint32_t intr_batch_cntr_thres_entries;
  869. /* Low threshold – in number of ring entries
  870. * (valid for src rings only)
  871. */
  872. uint32_t low_threshold;
  873. /* Misc flags */
  874. uint32_t flags;
  875. /* Unique ring id */
  876. uint8_t ring_id;
  877. /* Source or Destination ring */
  878. enum hal_srng_dir ring_dir;
  879. /* Size of ring entry */
  880. uint32_t entry_size;
  881. /* hw register base address */
  882. void *hwreg_base[MAX_SRNG_REG_GROUPS];
  883. /* prefetch timer config - in micro seconds */
  884. uint32_t prefetch_timer;
  885. };
  886. /* hal_construct_srng_shadow_regs() - initialize the shadow
  887. * registers for srngs
  888. * @hal_soc: hal handle
  889. *
  890. * Return: QDF_STATUS_OK on success
  891. */
  892. QDF_STATUS hal_construct_srng_shadow_regs(void *hal_soc);
  893. /* hal_set_one_shadow_config() - add a config for the specified ring
  894. * @hal_soc: hal handle
  895. * @ring_type: ring type
  896. * @ring_num: ring num
  897. *
  898. * The ring type and ring num uniquely specify the ring. After this call,
  899. * the hp/tp will be added as the next entry int the shadow register
  900. * configuration table. The hal code will use the shadow register address
  901. * in place of the hp/tp address.
  902. *
  903. * This function is exposed, so that the CE module can skip configuring shadow
  904. * registers for unused ring and rings assigned to the firmware.
  905. *
  906. * Return: QDF_STATUS_OK on success
  907. */
  908. QDF_STATUS hal_set_one_shadow_config(void *hal_soc, int ring_type,
  909. int ring_num);
  910. /**
  911. * hal_get_shadow_config() - retrieve the config table
  912. * @hal_soc: hal handle
  913. * @shadow_config: will point to the table after
  914. * @num_shadow_registers_configured: will contain the number of valid entries
  915. */
  916. extern void hal_get_shadow_config(void *hal_soc,
  917. struct pld_shadow_reg_v2_cfg **shadow_config,
  918. int *num_shadow_registers_configured);
  919. /**
  920. * hal_srng_setup - Initialize HW SRNG ring.
  921. *
  922. * @hal_soc: Opaque HAL SOC handle
  923. * @ring_type: one of the types from hal_ring_type
  924. * @ring_num: Ring number if there are multiple rings of
  925. * same type (staring from 0)
  926. * @mac_id: valid MAC Id should be passed if ring type is one of lmac rings
  927. * @ring_params: SRNG ring params in hal_srng_params structure.
  928. * Callers are expected to allocate contiguous ring memory of size
  929. * 'num_entries * entry_size' bytes and pass the physical and virtual base
  930. * addresses through 'ring_base_paddr' and 'ring_base_vaddr' in hal_srng_params
  931. * structure. Ring base address should be 8 byte aligned and size of each ring
  932. * entry should be queried using the API hal_srng_get_entrysize
  933. *
  934. * Return: Opaque pointer to ring on success
  935. * NULL on failure (if given ring is not available)
  936. */
  937. extern void *hal_srng_setup(void *hal_soc, int ring_type, int ring_num,
  938. int mac_id, struct hal_srng_params *ring_params);
  939. /* Remapping ids of REO rings */
  940. #define REO_REMAP_TCL 0
  941. #define REO_REMAP_SW1 1
  942. #define REO_REMAP_SW2 2
  943. #define REO_REMAP_SW3 3
  944. #define REO_REMAP_SW4 4
  945. #define REO_REMAP_RELEASE 5
  946. #define REO_REMAP_FW 6
  947. #define REO_REMAP_UNUSED 7
  948. /*
  949. * Macro to access HWIO_REO_R0_ERROR_DESTINATION_RING_CTRL_IX_0
  950. * to map destination to rings
  951. */
  952. #define HAL_REO_ERR_REMAP_IX0(_VALUE, _OFFSET) \
  953. ((_VALUE) << \
  954. (HWIO_REO_R0_ERROR_DESTINATION_MAPPING_IX_0_ERROR_ ## \
  955. DESTINATION_RING_ ## _OFFSET ## _SHFT))
  956. /*
  957. * Macro to access HWIO_REO_R0_ERROR_DESTINATION_RING_CTRL_IX_1
  958. * to map destination to rings
  959. */
  960. #define HAL_REO_ERR_REMAP_IX1(_VALUE, _OFFSET) \
  961. ((_VALUE) << \
  962. (HWIO_REO_R0_ERROR_DESTINATION_MAPPING_IX_1_ERROR_ ## \
  963. DESTINATION_RING_ ## _OFFSET ## _SHFT))
  964. /*
  965. * Macro to access HWIO_REO_R0_DESTINATION_RING_CTRL_IX_0
  966. * to map destination to rings
  967. */
  968. #define HAL_REO_REMAP_IX0(_VALUE, _OFFSET) \
  969. ((_VALUE) << \
  970. (HWIO_REO_R0_DESTINATION_RING_CTRL_IX_0_DEST_RING_MAPPING_ ## \
  971. _OFFSET ## _SHFT))
  972. /*
  973. * Macro to access HWIO_REO_R0_DESTINATION_RING_CTRL_IX_1
  974. * to map destination to rings
  975. */
  976. #define HAL_REO_REMAP_IX2(_VALUE, _OFFSET) \
  977. ((_VALUE) << \
  978. (HWIO_REO_R0_DESTINATION_RING_CTRL_IX_2_DEST_RING_MAPPING_ ## \
  979. _OFFSET ## _SHFT))
  980. /*
  981. * Macro to access HWIO_REO_R0_DESTINATION_RING_CTRL_IX_3
  982. * to map destination to rings
  983. */
  984. #define HAL_REO_REMAP_IX3(_VALUE, _OFFSET) \
  985. ((_VALUE) << \
  986. (HWIO_REO_R0_DESTINATION_RING_CTRL_IX_3_DEST_RING_MAPPING_ ## \
  987. _OFFSET ## _SHFT))
  988. /**
  989. * hal_reo_read_write_ctrl_ix - Read or write REO_DESTINATION_RING_CTRL_IX
  990. * @hal_soc_hdl: HAL SOC handle
  991. * @read: boolean value to indicate if read or write
  992. * @ix0: pointer to store IX0 reg value
  993. * @ix1: pointer to store IX1 reg value
  994. * @ix2: pointer to store IX2 reg value
  995. * @ix3: pointer to store IX3 reg value
  996. */
  997. void hal_reo_read_write_ctrl_ix(hal_soc_handle_t hal_soc_hdl, bool read,
  998. uint32_t *ix0, uint32_t *ix1,
  999. uint32_t *ix2, uint32_t *ix3);
  1000. /**
  1001. * hal_srng_set_hp_paddr_confirm() - Set physical address to dest SRNG head
  1002. * pointer and confirm that write went through by reading back the value
  1003. * @sring: sring pointer
  1004. * @paddr: physical address
  1005. *
  1006. * Return: None
  1007. */
  1008. extern void hal_srng_dst_set_hp_paddr_confirm(struct hal_srng *sring,
  1009. uint64_t paddr);
  1010. /**
  1011. * hal_srng_dst_init_hp() - Initilaize head pointer with cached head pointer
  1012. * @hal_soc: hal_soc handle
  1013. * @srng: sring pointer
  1014. * @vaddr: virtual address
  1015. */
  1016. void hal_srng_dst_init_hp(struct hal_soc_handle *hal_soc,
  1017. struct hal_srng *srng,
  1018. uint32_t *vaddr);
  1019. /**
  1020. * hal_srng_cleanup - Deinitialize HW SRNG ring.
  1021. * @hal_soc: Opaque HAL SOC handle
  1022. * @hal_srng: Opaque HAL SRNG pointer
  1023. */
  1024. void hal_srng_cleanup(void *hal_soc, hal_ring_handle_t hal_ring_hdl);
  1025. static inline bool hal_srng_initialized(hal_ring_handle_t hal_ring_hdl)
  1026. {
  1027. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1028. return !!srng->initialized;
  1029. }
  1030. /**
  1031. * hal_srng_dst_peek - Check if there are any entries in the ring (peek)
  1032. * @hal_soc: Opaque HAL SOC handle
  1033. * @hal_ring_hdl: Destination ring pointer
  1034. *
  1035. * Caller takes responsibility for any locking needs.
  1036. *
  1037. * Return: Opaque pointer for next ring entry; NULL on failire
  1038. */
  1039. static inline
  1040. void *hal_srng_dst_peek(hal_soc_handle_t hal_soc_hdl,
  1041. hal_ring_handle_t hal_ring_hdl)
  1042. {
  1043. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1044. if (srng->u.dst_ring.tp != srng->u.dst_ring.cached_hp)
  1045. return (void *)(&srng->ring_base_vaddr[srng->u.dst_ring.tp]);
  1046. return NULL;
  1047. }
  1048. /**
  1049. * hal_mem_dma_cache_sync - Cache sync the specified virtual address Range
  1050. * @hal_soc: HAL soc handle
  1051. * @desc: desc start address
  1052. * @entry_size: size of memory to sync
  1053. *
  1054. * Return: void
  1055. */
  1056. #if defined(__LINUX_MIPS32_ARCH__) || defined(__LINUX_MIPS64_ARCH__)
  1057. static inline void hal_mem_dma_cache_sync(struct hal_soc *soc, uint32_t *desc,
  1058. uint32_t entry_size)
  1059. {
  1060. qdf_nbuf_dma_inv_range((void *)desc, (void *)(desc + entry_size));
  1061. }
  1062. #else
  1063. static inline void hal_mem_dma_cache_sync(struct hal_soc *soc, uint32_t *desc,
  1064. uint32_t entry_size)
  1065. {
  1066. qdf_mem_dma_cache_sync(soc->qdf_dev, qdf_mem_virt_to_phys(desc),
  1067. QDF_DMA_FROM_DEVICE,
  1068. (entry_size * sizeof(uint32_t)));
  1069. }
  1070. #endif
  1071. /**
  1072. * hal_srng_access_start_unlocked - Start ring access (unlocked). Should use
  1073. * hal_srng_access_start if locked access is required
  1074. *
  1075. * @hal_soc: Opaque HAL SOC handle
  1076. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  1077. *
  1078. * This API doesn't implement any byte-order conversion on reading hp/tp.
  1079. * So, Use API only for those srngs for which the target writes hp/tp values to
  1080. * the DDR in the Host order.
  1081. *
  1082. * Return: 0 on success; error on failire
  1083. */
  1084. static inline int
  1085. hal_srng_access_start_unlocked(hal_soc_handle_t hal_soc_hdl,
  1086. hal_ring_handle_t hal_ring_hdl)
  1087. {
  1088. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1089. struct hal_soc *soc = (struct hal_soc *)hal_soc_hdl;
  1090. uint32_t *desc;
  1091. if (srng->ring_dir == HAL_SRNG_SRC_RING)
  1092. srng->u.src_ring.cached_tp =
  1093. *(volatile uint32_t *)(srng->u.src_ring.tp_addr);
  1094. else {
  1095. srng->u.dst_ring.cached_hp =
  1096. *(volatile uint32_t *)(srng->u.dst_ring.hp_addr);
  1097. if (srng->flags & HAL_SRNG_CACHED_DESC) {
  1098. desc = hal_srng_dst_peek(hal_soc_hdl, hal_ring_hdl);
  1099. if (qdf_likely(desc)) {
  1100. hal_mem_dma_cache_sync(soc, desc,
  1101. srng->entry_size);
  1102. qdf_prefetch(desc);
  1103. }
  1104. }
  1105. }
  1106. return 0;
  1107. }
  1108. /**
  1109. * hal_le_srng_access_start_unlocked_in_cpu_order - Start ring access
  1110. * (unlocked) with endianness correction.
  1111. * @hal_soc: Opaque HAL SOC handle
  1112. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  1113. *
  1114. * This API provides same functionally as hal_srng_access_start_unlocked()
  1115. * except that it converts the little-endian formatted hp/tp values to
  1116. * Host order on reading them. So, this API should only be used for those srngs
  1117. * for which the target always writes hp/tp values in little-endian order
  1118. * regardless of Host order.
  1119. *
  1120. * Also, this API doesn't take the lock. For locked access, use
  1121. * hal_srng_access_start/hal_le_srng_access_start_in_cpu_order.
  1122. *
  1123. * Return: 0 on success; error on failire
  1124. */
  1125. static inline int
  1126. hal_le_srng_access_start_unlocked_in_cpu_order(
  1127. hal_soc_handle_t hal_soc_hdl,
  1128. hal_ring_handle_t hal_ring_hdl)
  1129. {
  1130. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1131. struct hal_soc *soc = (struct hal_soc *)hal_soc_hdl;
  1132. uint32_t *desc;
  1133. if (srng->ring_dir == HAL_SRNG_SRC_RING)
  1134. srng->u.src_ring.cached_tp =
  1135. qdf_le32_to_cpu(*(volatile uint32_t *)
  1136. (srng->u.src_ring.tp_addr));
  1137. else {
  1138. srng->u.dst_ring.cached_hp =
  1139. qdf_le32_to_cpu(*(volatile uint32_t *)
  1140. (srng->u.dst_ring.hp_addr));
  1141. if (srng->flags & HAL_SRNG_CACHED_DESC) {
  1142. desc = hal_srng_dst_peek(hal_soc_hdl, hal_ring_hdl);
  1143. if (qdf_likely(desc)) {
  1144. hal_mem_dma_cache_sync(soc, desc,
  1145. srng->entry_size);
  1146. qdf_prefetch(desc);
  1147. }
  1148. }
  1149. }
  1150. return 0;
  1151. }
  1152. /**
  1153. * hal_srng_try_access_start - Try to start (locked) ring access
  1154. *
  1155. * @hal_soc: Opaque HAL SOC handle
  1156. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  1157. *
  1158. * Return: 0 on success; error on failure
  1159. */
  1160. static inline int hal_srng_try_access_start(hal_soc_handle_t hal_soc_hdl,
  1161. hal_ring_handle_t hal_ring_hdl)
  1162. {
  1163. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1164. if (qdf_unlikely(!hal_ring_hdl)) {
  1165. qdf_print("Error: Invalid hal_ring\n");
  1166. return -EINVAL;
  1167. }
  1168. if (!SRNG_TRY_LOCK(&(srng->lock)))
  1169. return -EINVAL;
  1170. return hal_srng_access_start_unlocked(hal_soc_hdl, hal_ring_hdl);
  1171. }
  1172. /**
  1173. * hal_srng_access_start - Start (locked) ring access
  1174. *
  1175. * @hal_soc: Opaque HAL SOC handle
  1176. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  1177. *
  1178. * This API doesn't implement any byte-order conversion on reading hp/tp.
  1179. * So, Use API only for those srngs for which the target writes hp/tp values to
  1180. * the DDR in the Host order.
  1181. *
  1182. * Return: 0 on success; error on failire
  1183. */
  1184. static inline int hal_srng_access_start(hal_soc_handle_t hal_soc_hdl,
  1185. hal_ring_handle_t hal_ring_hdl)
  1186. {
  1187. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1188. if (qdf_unlikely(!hal_ring_hdl)) {
  1189. qdf_print("Error: Invalid hal_ring\n");
  1190. return -EINVAL;
  1191. }
  1192. SRNG_LOCK(&(srng->lock));
  1193. return hal_srng_access_start_unlocked(hal_soc_hdl, hal_ring_hdl);
  1194. }
  1195. /**
  1196. * hal_le_srng_access_start_in_cpu_order - Start (locked) ring access with
  1197. * endianness correction
  1198. * @hal_soc: Opaque HAL SOC handle
  1199. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  1200. *
  1201. * This API provides same functionally as hal_srng_access_start()
  1202. * except that it converts the little-endian formatted hp/tp values to
  1203. * Host order on reading them. So, this API should only be used for those srngs
  1204. * for which the target always writes hp/tp values in little-endian order
  1205. * regardless of Host order.
  1206. *
  1207. * Return: 0 on success; error on failire
  1208. */
  1209. static inline int
  1210. hal_le_srng_access_start_in_cpu_order(
  1211. hal_soc_handle_t hal_soc_hdl,
  1212. hal_ring_handle_t hal_ring_hdl)
  1213. {
  1214. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1215. if (qdf_unlikely(!hal_ring_hdl)) {
  1216. qdf_print("Error: Invalid hal_ring\n");
  1217. return -EINVAL;
  1218. }
  1219. SRNG_LOCK(&(srng->lock));
  1220. return hal_le_srng_access_start_unlocked_in_cpu_order(
  1221. hal_soc_hdl, hal_ring_hdl);
  1222. }
  1223. /**
  1224. * hal_srng_dst_get_next - Get next entry from a destination ring
  1225. * @hal_soc: Opaque HAL SOC handle
  1226. * @hal_ring_hdl: Destination ring pointer
  1227. *
  1228. * Return: Opaque pointer for next ring entry; NULL on failure
  1229. */
  1230. static inline
  1231. void *hal_srng_dst_get_next(void *hal_soc,
  1232. hal_ring_handle_t hal_ring_hdl)
  1233. {
  1234. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1235. uint32_t *desc;
  1236. if (srng->u.dst_ring.tp == srng->u.dst_ring.cached_hp)
  1237. return NULL;
  1238. desc = &srng->ring_base_vaddr[srng->u.dst_ring.tp];
  1239. /* TODO: Using % is expensive, but we have to do this since
  1240. * size of some SRNG rings is not power of 2 (due to descriptor
  1241. * sizes). Need to create separate API for rings used
  1242. * per-packet, with sizes power of 2 (TCL2SW, REO2SW,
  1243. * SW2RXDMA and CE rings)
  1244. */
  1245. srng->u.dst_ring.tp = (srng->u.dst_ring.tp + srng->entry_size);
  1246. if (srng->u.dst_ring.tp == srng->ring_size)
  1247. srng->u.dst_ring.tp = 0;
  1248. if (srng->flags & HAL_SRNG_CACHED_DESC) {
  1249. struct hal_soc *soc = (struct hal_soc *)hal_soc;
  1250. uint32_t *desc_next;
  1251. uint32_t tp;
  1252. tp = srng->u.dst_ring.tp;
  1253. desc_next = &srng->ring_base_vaddr[srng->u.dst_ring.tp];
  1254. hal_mem_dma_cache_sync(soc, desc_next, srng->entry_size);
  1255. qdf_prefetch(desc_next);
  1256. }
  1257. return (void *)desc;
  1258. }
  1259. /**
  1260. * hal_srng_dst_get_next_cached - Get cached next entry
  1261. * @hal_soc: Opaque HAL SOC handle
  1262. * @hal_ring_hdl: Destination ring pointer
  1263. *
  1264. * Get next entry from a destination ring and move cached tail pointer
  1265. *
  1266. * Return: Opaque pointer for next ring entry; NULL on failure
  1267. */
  1268. static inline
  1269. void *hal_srng_dst_get_next_cached(void *hal_soc,
  1270. hal_ring_handle_t hal_ring_hdl)
  1271. {
  1272. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1273. uint32_t *desc;
  1274. uint32_t *desc_next;
  1275. if (srng->u.dst_ring.tp == srng->u.dst_ring.cached_hp)
  1276. return NULL;
  1277. desc = &srng->ring_base_vaddr[srng->u.dst_ring.tp];
  1278. /* TODO: Using % is expensive, but we have to do this since
  1279. * size of some SRNG rings is not power of 2 (due to descriptor
  1280. * sizes). Need to create separate API for rings used
  1281. * per-packet, with sizes power of 2 (TCL2SW, REO2SW,
  1282. * SW2RXDMA and CE rings)
  1283. */
  1284. srng->u.dst_ring.tp = (srng->u.dst_ring.tp + srng->entry_size);
  1285. if (srng->u.dst_ring.tp == srng->ring_size)
  1286. srng->u.dst_ring.tp = 0;
  1287. desc_next = &srng->ring_base_vaddr[srng->u.dst_ring.tp];
  1288. qdf_prefetch(desc_next);
  1289. return (void *)desc;
  1290. }
  1291. /**
  1292. * hal_srng_dst_get_next_hp - Get next entry from a destination ring and move
  1293. * cached head pointer
  1294. *
  1295. * @hal_soc: Opaque HAL SOC handle
  1296. * @hal_ring_hdl: Destination ring pointer
  1297. *
  1298. * Return: Opaque pointer for next ring entry; NULL on failire
  1299. */
  1300. static inline void *
  1301. hal_srng_dst_get_next_hp(hal_soc_handle_t hal_soc_hdl,
  1302. hal_ring_handle_t hal_ring_hdl)
  1303. {
  1304. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1305. uint32_t *desc;
  1306. /* TODO: Using % is expensive, but we have to do this since
  1307. * size of some SRNG rings is not power of 2 (due to descriptor
  1308. * sizes). Need to create separate API for rings used
  1309. * per-packet, with sizes power of 2 (TCL2SW, REO2SW,
  1310. * SW2RXDMA and CE rings)
  1311. */
  1312. uint32_t next_hp = (srng->u.dst_ring.cached_hp + srng->entry_size) %
  1313. srng->ring_size;
  1314. if (next_hp != srng->u.dst_ring.tp) {
  1315. desc = &(srng->ring_base_vaddr[srng->u.dst_ring.cached_hp]);
  1316. srng->u.dst_ring.cached_hp = next_hp;
  1317. return (void *)desc;
  1318. }
  1319. return NULL;
  1320. }
  1321. /**
  1322. * hal_srng_dst_peek_sync - Check if there are any entries in the ring (peek)
  1323. * @hal_soc: Opaque HAL SOC handle
  1324. * @hal_ring_hdl: Destination ring pointer
  1325. *
  1326. * Sync cached head pointer with HW.
  1327. * Caller takes responsibility for any locking needs.
  1328. *
  1329. * Return: Opaque pointer for next ring entry; NULL on failire
  1330. */
  1331. static inline
  1332. void *hal_srng_dst_peek_sync(hal_soc_handle_t hal_soc_hdl,
  1333. hal_ring_handle_t hal_ring_hdl)
  1334. {
  1335. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1336. srng->u.dst_ring.cached_hp =
  1337. *(volatile uint32_t *)(srng->u.dst_ring.hp_addr);
  1338. if (srng->u.dst_ring.tp != srng->u.dst_ring.cached_hp)
  1339. return (void *)(&(srng->ring_base_vaddr[srng->u.dst_ring.tp]));
  1340. return NULL;
  1341. }
  1342. /**
  1343. * hal_srng_dst_peek_sync_locked - Peek for any entries in the ring
  1344. * @hal_soc: Opaque HAL SOC handle
  1345. * @hal_ring_hdl: Destination ring pointer
  1346. *
  1347. * Sync cached head pointer with HW.
  1348. * This function takes up SRNG_LOCK. Should not be called with SRNG lock held.
  1349. *
  1350. * Return: Opaque pointer for next ring entry; NULL on failire
  1351. */
  1352. static inline
  1353. void *hal_srng_dst_peek_sync_locked(hal_soc_handle_t hal_soc_hdl,
  1354. hal_ring_handle_t hal_ring_hdl)
  1355. {
  1356. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1357. void *ring_desc_ptr = NULL;
  1358. if (qdf_unlikely(!hal_ring_hdl)) {
  1359. qdf_print("Error: Invalid hal_ring\n");
  1360. return NULL;
  1361. }
  1362. SRNG_LOCK(&srng->lock);
  1363. ring_desc_ptr = hal_srng_dst_peek_sync(hal_soc_hdl, hal_ring_hdl);
  1364. SRNG_UNLOCK(&srng->lock);
  1365. return ring_desc_ptr;
  1366. }
  1367. /**
  1368. * hal_srng_dst_num_valid - Returns number of valid entries (to be processed
  1369. * by SW) in destination ring
  1370. *
  1371. * @hal_soc: Opaque HAL SOC handle
  1372. * @hal_ring_hdl: Destination ring pointer
  1373. * @sync_hw_ptr: Sync cached head pointer with HW
  1374. *
  1375. */
  1376. static inline
  1377. uint32_t hal_srng_dst_num_valid(void *hal_soc,
  1378. hal_ring_handle_t hal_ring_hdl,
  1379. int sync_hw_ptr)
  1380. {
  1381. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1382. uint32_t hp;
  1383. uint32_t tp = srng->u.dst_ring.tp;
  1384. if (sync_hw_ptr) {
  1385. hp = *(volatile uint32_t *)(srng->u.dst_ring.hp_addr);
  1386. srng->u.dst_ring.cached_hp = hp;
  1387. } else {
  1388. hp = srng->u.dst_ring.cached_hp;
  1389. }
  1390. if (hp >= tp)
  1391. return (hp - tp) / srng->entry_size;
  1392. return (srng->ring_size - tp + hp) / srng->entry_size;
  1393. }
  1394. /**
  1395. * hal_srng_dst_inv_cached_descs - API to invalidate descriptors in batch mode
  1396. * @hal_soc: Opaque HAL SOC handle
  1397. * @hal_ring_hdl: Destination ring pointer
  1398. * @entry_count: Number of descriptors to be invalidated
  1399. *
  1400. * Invalidates a set of cached descriptors starting from tail to
  1401. * provided count worth
  1402. *
  1403. * Return - None
  1404. */
  1405. static inline void hal_srng_dst_inv_cached_descs(void *hal_soc,
  1406. hal_ring_handle_t hal_ring_hdl,
  1407. uint32_t entry_count)
  1408. {
  1409. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1410. uint32_t hp = srng->u.dst_ring.cached_hp;
  1411. uint32_t tp = srng->u.dst_ring.tp;
  1412. uint32_t sync_p = 0;
  1413. /*
  1414. * If SRNG does not have cached descriptors this
  1415. * API call should be a no op
  1416. */
  1417. if (!(srng->flags & HAL_SRNG_CACHED_DESC))
  1418. return;
  1419. if (qdf_unlikely(entry_count == 0))
  1420. return;
  1421. sync_p = (entry_count - 1) * srng->entry_size;
  1422. if (hp > tp) {
  1423. qdf_nbuf_dma_inv_range(&srng->ring_base_vaddr[tp],
  1424. &srng->ring_base_vaddr[tp + sync_p]
  1425. + (srng->entry_size * sizeof(uint32_t)));
  1426. } else {
  1427. /*
  1428. * We have wrapped around
  1429. */
  1430. uint32_t wrap_cnt = ((srng->ring_size - tp) / srng->entry_size);
  1431. if (entry_count <= wrap_cnt) {
  1432. qdf_nbuf_dma_inv_range(&srng->ring_base_vaddr[tp],
  1433. &srng->ring_base_vaddr[tp + sync_p] +
  1434. (srng->entry_size * sizeof(uint32_t)));
  1435. return;
  1436. }
  1437. entry_count -= wrap_cnt;
  1438. sync_p = (entry_count - 1) * srng->entry_size;
  1439. qdf_nbuf_dma_inv_range(&srng->ring_base_vaddr[tp],
  1440. &srng->ring_base_vaddr[srng->ring_size - srng->entry_size] +
  1441. (srng->entry_size * sizeof(uint32_t)));
  1442. qdf_nbuf_dma_inv_range(&srng->ring_base_vaddr[0],
  1443. &srng->ring_base_vaddr[sync_p]
  1444. + (srng->entry_size * sizeof(uint32_t)));
  1445. }
  1446. }
  1447. /**
  1448. * hal_srng_dst_num_valid_locked - Returns num valid entries to be processed
  1449. *
  1450. * @hal_soc: Opaque HAL SOC handle
  1451. * @hal_ring_hdl: Destination ring pointer
  1452. * @sync_hw_ptr: Sync cached head pointer with HW
  1453. *
  1454. * Returns number of valid entries to be processed by the host driver. The
  1455. * function takes up SRNG lock.
  1456. *
  1457. * Return: Number of valid destination entries
  1458. */
  1459. static inline uint32_t
  1460. hal_srng_dst_num_valid_locked(hal_soc_handle_t hal_soc,
  1461. hal_ring_handle_t hal_ring_hdl,
  1462. int sync_hw_ptr)
  1463. {
  1464. uint32_t num_valid;
  1465. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1466. SRNG_LOCK(&srng->lock);
  1467. num_valid = hal_srng_dst_num_valid(hal_soc, hal_ring_hdl, sync_hw_ptr);
  1468. SRNG_UNLOCK(&srng->lock);
  1469. return num_valid;
  1470. }
  1471. /**
  1472. * hal_srng_sync_cachedhp - sync cachehp pointer from hw hp
  1473. *
  1474. * @hal_soc: Opaque HAL SOC handle
  1475. * @hal_ring_hdl: Destination ring pointer
  1476. *
  1477. */
  1478. static inline
  1479. void hal_srng_sync_cachedhp(void *hal_soc,
  1480. hal_ring_handle_t hal_ring_hdl)
  1481. {
  1482. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1483. uint32_t hp;
  1484. hp = *(volatile uint32_t *)(srng->u.dst_ring.hp_addr);
  1485. srng->u.dst_ring.cached_hp = hp;
  1486. }
  1487. /**
  1488. * hal_srng_src_reap_next - Reap next entry from a source ring and move reap
  1489. * pointer. This can be used to release any buffers associated with completed
  1490. * ring entries. Note that this should not be used for posting new descriptor
  1491. * entries. Posting of new entries should be done only using
  1492. * hal_srng_src_get_next_reaped when this function is used for reaping.
  1493. *
  1494. * @hal_soc: Opaque HAL SOC handle
  1495. * @hal_ring_hdl: Source ring pointer
  1496. *
  1497. * Return: Opaque pointer for next ring entry; NULL on failire
  1498. */
  1499. static inline void *
  1500. hal_srng_src_reap_next(void *hal_soc, hal_ring_handle_t hal_ring_hdl)
  1501. {
  1502. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1503. uint32_t *desc;
  1504. /* TODO: Using % is expensive, but we have to do this since
  1505. * size of some SRNG rings is not power of 2 (due to descriptor
  1506. * sizes). Need to create separate API for rings used
  1507. * per-packet, with sizes power of 2 (TCL2SW, REO2SW,
  1508. * SW2RXDMA and CE rings)
  1509. */
  1510. uint32_t next_reap_hp = (srng->u.src_ring.reap_hp + srng->entry_size) %
  1511. srng->ring_size;
  1512. if (next_reap_hp != srng->u.src_ring.cached_tp) {
  1513. desc = &(srng->ring_base_vaddr[next_reap_hp]);
  1514. srng->u.src_ring.reap_hp = next_reap_hp;
  1515. return (void *)desc;
  1516. }
  1517. return NULL;
  1518. }
  1519. /**
  1520. * hal_srng_src_get_next_reaped - Get next entry from a source ring that is
  1521. * already reaped using hal_srng_src_reap_next, for posting new entries to
  1522. * the ring
  1523. *
  1524. * @hal_soc: Opaque HAL SOC handle
  1525. * @hal_ring_hdl: Source ring pointer
  1526. *
  1527. * Return: Opaque pointer for next (reaped) source ring entry; NULL on failire
  1528. */
  1529. static inline void *
  1530. hal_srng_src_get_next_reaped(void *hal_soc, hal_ring_handle_t hal_ring_hdl)
  1531. {
  1532. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1533. uint32_t *desc;
  1534. if (srng->u.src_ring.hp != srng->u.src_ring.reap_hp) {
  1535. desc = &(srng->ring_base_vaddr[srng->u.src_ring.hp]);
  1536. srng->u.src_ring.hp = (srng->u.src_ring.hp + srng->entry_size) %
  1537. srng->ring_size;
  1538. return (void *)desc;
  1539. }
  1540. return NULL;
  1541. }
  1542. /**
  1543. * hal_srng_src_pending_reap_next - Reap next entry from a source ring and
  1544. * move reap pointer. This API is used in detach path to release any buffers
  1545. * associated with ring entries which are pending reap.
  1546. *
  1547. * @hal_soc: Opaque HAL SOC handle
  1548. * @hal_ring_hdl: Source ring pointer
  1549. *
  1550. * Return: Opaque pointer for next ring entry; NULL on failire
  1551. */
  1552. static inline void *
  1553. hal_srng_src_pending_reap_next(void *hal_soc, hal_ring_handle_t hal_ring_hdl)
  1554. {
  1555. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1556. uint32_t *desc;
  1557. uint32_t next_reap_hp = (srng->u.src_ring.reap_hp + srng->entry_size) %
  1558. srng->ring_size;
  1559. if (next_reap_hp != srng->u.src_ring.hp) {
  1560. desc = &(srng->ring_base_vaddr[next_reap_hp]);
  1561. srng->u.src_ring.reap_hp = next_reap_hp;
  1562. return (void *)desc;
  1563. }
  1564. return NULL;
  1565. }
  1566. /**
  1567. * hal_srng_src_done_val -
  1568. *
  1569. * @hal_soc: Opaque HAL SOC handle
  1570. * @hal_ring_hdl: Source ring pointer
  1571. *
  1572. * Return: Opaque pointer for next ring entry; NULL on failire
  1573. */
  1574. static inline uint32_t
  1575. hal_srng_src_done_val(void *hal_soc, hal_ring_handle_t hal_ring_hdl)
  1576. {
  1577. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1578. /* TODO: Using % is expensive, but we have to do this since
  1579. * size of some SRNG rings is not power of 2 (due to descriptor
  1580. * sizes). Need to create separate API for rings used
  1581. * per-packet, with sizes power of 2 (TCL2SW, REO2SW,
  1582. * SW2RXDMA and CE rings)
  1583. */
  1584. uint32_t next_reap_hp = (srng->u.src_ring.reap_hp + srng->entry_size) %
  1585. srng->ring_size;
  1586. if (next_reap_hp == srng->u.src_ring.cached_tp)
  1587. return 0;
  1588. if (srng->u.src_ring.cached_tp > next_reap_hp)
  1589. return (srng->u.src_ring.cached_tp - next_reap_hp) /
  1590. srng->entry_size;
  1591. else
  1592. return ((srng->ring_size - next_reap_hp) +
  1593. srng->u.src_ring.cached_tp) / srng->entry_size;
  1594. }
  1595. /**
  1596. * hal_get_entrysize_from_srng() - Retrieve ring entry size
  1597. * @hal_ring_hdl: Source ring pointer
  1598. *
  1599. * Return: uint8_t
  1600. */
  1601. static inline
  1602. uint8_t hal_get_entrysize_from_srng(hal_ring_handle_t hal_ring_hdl)
  1603. {
  1604. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1605. return srng->entry_size;
  1606. }
  1607. /**
  1608. * hal_get_sw_hptp - Get SW head and tail pointer location for any ring
  1609. * @hal_soc: Opaque HAL SOC handle
  1610. * @hal_ring_hdl: Source ring pointer
  1611. * @tailp: Tail Pointer
  1612. * @headp: Head Pointer
  1613. *
  1614. * Return: Update tail pointer and head pointer in arguments.
  1615. */
  1616. static inline
  1617. void hal_get_sw_hptp(void *hal_soc, hal_ring_handle_t hal_ring_hdl,
  1618. uint32_t *tailp, uint32_t *headp)
  1619. {
  1620. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1621. if (srng->ring_dir == HAL_SRNG_SRC_RING) {
  1622. *headp = srng->u.src_ring.hp;
  1623. *tailp = *srng->u.src_ring.tp_addr;
  1624. } else {
  1625. *tailp = srng->u.dst_ring.tp;
  1626. *headp = *srng->u.dst_ring.hp_addr;
  1627. }
  1628. }
  1629. /**
  1630. * hal_srng_src_get_next - Get next entry from a source ring and move cached tail pointer
  1631. *
  1632. * @hal_soc: Opaque HAL SOC handle
  1633. * @hal_ring_hdl: Source ring pointer
  1634. *
  1635. * Return: Opaque pointer for next ring entry; NULL on failire
  1636. */
  1637. static inline
  1638. void *hal_srng_src_get_next(void *hal_soc,
  1639. hal_ring_handle_t hal_ring_hdl)
  1640. {
  1641. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1642. uint32_t *desc;
  1643. /* TODO: Using % is expensive, but we have to do this since
  1644. * size of some SRNG rings is not power of 2 (due to descriptor
  1645. * sizes). Need to create separate API for rings used
  1646. * per-packet, with sizes power of 2 (TCL2SW, REO2SW,
  1647. * SW2RXDMA and CE rings)
  1648. */
  1649. uint32_t next_hp = (srng->u.src_ring.hp + srng->entry_size) %
  1650. srng->ring_size;
  1651. if (next_hp != srng->u.src_ring.cached_tp) {
  1652. desc = &(srng->ring_base_vaddr[srng->u.src_ring.hp]);
  1653. srng->u.src_ring.hp = next_hp;
  1654. /* TODO: Since reap function is not used by all rings, we can
  1655. * remove the following update of reap_hp in this function
  1656. * if we can ensure that only hal_srng_src_get_next_reaped
  1657. * is used for the rings requiring reap functionality
  1658. */
  1659. srng->u.src_ring.reap_hp = next_hp;
  1660. return (void *)desc;
  1661. }
  1662. return NULL;
  1663. }
  1664. /**
  1665. * hal_srng_src_peek_n_get_next - Get next entry from a ring without
  1666. * moving head pointer.
  1667. * hal_srng_src_get_next should be called subsequently to move the head pointer
  1668. *
  1669. * @hal_soc: Opaque HAL SOC handle
  1670. * @hal_ring_hdl: Source ring pointer
  1671. *
  1672. * Return: Opaque pointer for next ring entry; NULL on failire
  1673. */
  1674. static inline
  1675. void *hal_srng_src_peek_n_get_next(hal_soc_handle_t hal_soc_hdl,
  1676. hal_ring_handle_t hal_ring_hdl)
  1677. {
  1678. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1679. uint32_t *desc;
  1680. /* TODO: Using % is expensive, but we have to do this since
  1681. * size of some SRNG rings is not power of 2 (due to descriptor
  1682. * sizes). Need to create separate API for rings used
  1683. * per-packet, with sizes power of 2 (TCL2SW, REO2SW,
  1684. * SW2RXDMA and CE rings)
  1685. */
  1686. if (((srng->u.src_ring.hp + srng->entry_size) %
  1687. srng->ring_size) != srng->u.src_ring.cached_tp) {
  1688. desc = &(srng->ring_base_vaddr[(srng->u.src_ring.hp +
  1689. srng->entry_size) %
  1690. srng->ring_size]);
  1691. return (void *)desc;
  1692. }
  1693. return NULL;
  1694. }
  1695. /**
  1696. * hal_srng_src_peek_n_get_next_next - Get next to next, i.e HP + 2 entry
  1697. * from a ring without moving head pointer.
  1698. *
  1699. * @hal_soc: Opaque HAL SOC handle
  1700. * @hal_ring_hdl: Source ring pointer
  1701. *
  1702. * Return: Opaque pointer for next to next ring entry; NULL on failire
  1703. */
  1704. static inline
  1705. void *hal_srng_src_peek_n_get_next_next(hal_soc_handle_t hal_soc_hdl,
  1706. hal_ring_handle_t hal_ring_hdl)
  1707. {
  1708. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1709. uint32_t *desc;
  1710. /* TODO: Using % is expensive, but we have to do this since
  1711. * size of some SRNG rings is not power of 2 (due to descriptor
  1712. * sizes). Need to create separate API for rings used
  1713. * per-packet, with sizes power of 2 (TCL2SW, REO2SW,
  1714. * SW2RXDMA and CE rings)
  1715. */
  1716. if ((((srng->u.src_ring.hp + (srng->entry_size)) %
  1717. srng->ring_size) != srng->u.src_ring.cached_tp) &&
  1718. (((srng->u.src_ring.hp + (srng->entry_size * 2)) %
  1719. srng->ring_size) != srng->u.src_ring.cached_tp)) {
  1720. desc = &(srng->ring_base_vaddr[(srng->u.src_ring.hp +
  1721. (srng->entry_size * 2)) %
  1722. srng->ring_size]);
  1723. return (void *)desc;
  1724. }
  1725. return NULL;
  1726. }
  1727. /**
  1728. * hal_srng_src_get_cur_hp_n_move_next () - API returns current hp
  1729. * and move hp to next in src ring
  1730. *
  1731. * Usage: This API should only be used at init time replenish.
  1732. *
  1733. * @hal_soc_hdl: HAL soc handle
  1734. * @hal_ring_hdl: Source ring pointer
  1735. *
  1736. */
  1737. static inline void *
  1738. hal_srng_src_get_cur_hp_n_move_next(hal_soc_handle_t hal_soc_hdl,
  1739. hal_ring_handle_t hal_ring_hdl)
  1740. {
  1741. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1742. uint32_t *cur_desc = NULL;
  1743. uint32_t next_hp;
  1744. cur_desc = &srng->ring_base_vaddr[(srng->u.src_ring.hp)];
  1745. next_hp = (srng->u.src_ring.hp + srng->entry_size) %
  1746. srng->ring_size;
  1747. if (next_hp != srng->u.src_ring.cached_tp)
  1748. srng->u.src_ring.hp = next_hp;
  1749. return (void *)cur_desc;
  1750. }
  1751. /**
  1752. * hal_srng_src_num_avail - Returns number of available entries in src ring
  1753. *
  1754. * @hal_soc: Opaque HAL SOC handle
  1755. * @hal_ring_hdl: Source ring pointer
  1756. * @sync_hw_ptr: Sync cached tail pointer with HW
  1757. *
  1758. */
  1759. static inline uint32_t
  1760. hal_srng_src_num_avail(void *hal_soc,
  1761. hal_ring_handle_t hal_ring_hdl, int sync_hw_ptr)
  1762. {
  1763. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1764. uint32_t tp;
  1765. uint32_t hp = srng->u.src_ring.hp;
  1766. if (sync_hw_ptr) {
  1767. tp = *(srng->u.src_ring.tp_addr);
  1768. srng->u.src_ring.cached_tp = tp;
  1769. } else {
  1770. tp = srng->u.src_ring.cached_tp;
  1771. }
  1772. if (tp > hp)
  1773. return ((tp - hp) / srng->entry_size) - 1;
  1774. else
  1775. return ((srng->ring_size - hp + tp) / srng->entry_size) - 1;
  1776. }
  1777. /**
  1778. * hal_srng_access_end_unlocked - End ring access (unlocked) - update cached
  1779. * ring head/tail pointers to HW.
  1780. *
  1781. * @hal_soc: Opaque HAL SOC handle
  1782. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  1783. *
  1784. * The target expects cached head/tail pointer to be updated to the
  1785. * shared location in the little-endian order, This API ensures that.
  1786. * This API should be used only if hal_srng_access_start_unlocked was used to
  1787. * start ring access
  1788. *
  1789. * Return: None
  1790. */
  1791. static inline void
  1792. hal_srng_access_end_unlocked(void *hal_soc, hal_ring_handle_t hal_ring_hdl)
  1793. {
  1794. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1795. /* TODO: See if we need a write memory barrier here */
  1796. if (srng->flags & HAL_SRNG_LMAC_RING) {
  1797. /* For LMAC rings, ring pointer updates are done through FW and
  1798. * hence written to a shared memory location that is read by FW
  1799. */
  1800. if (srng->ring_dir == HAL_SRNG_SRC_RING) {
  1801. *srng->u.src_ring.hp_addr =
  1802. qdf_cpu_to_le32(srng->u.src_ring.hp);
  1803. } else {
  1804. *srng->u.dst_ring.tp_addr =
  1805. qdf_cpu_to_le32(srng->u.dst_ring.tp);
  1806. }
  1807. } else {
  1808. if (srng->ring_dir == HAL_SRNG_SRC_RING)
  1809. hal_srng_write_address_32_mb(hal_soc,
  1810. srng,
  1811. srng->u.src_ring.hp_addr,
  1812. srng->u.src_ring.hp);
  1813. else
  1814. hal_srng_write_address_32_mb(hal_soc,
  1815. srng,
  1816. srng->u.dst_ring.tp_addr,
  1817. srng->u.dst_ring.tp);
  1818. }
  1819. }
  1820. /* hal_srng_access_end_unlocked already handles endianness conversion,
  1821. * use the same.
  1822. */
  1823. #define hal_le_srng_access_end_unlocked_in_cpu_order \
  1824. hal_srng_access_end_unlocked
  1825. /**
  1826. * hal_srng_access_end - Unlock ring access and update cached ring head/tail
  1827. * pointers to HW
  1828. *
  1829. * @hal_soc: Opaque HAL SOC handle
  1830. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  1831. *
  1832. * The target expects cached head/tail pointer to be updated to the
  1833. * shared location in the little-endian order, This API ensures that.
  1834. * This API should be used only if hal_srng_access_start was used to
  1835. * start ring access
  1836. *
  1837. * Return: 0 on success; error on failire
  1838. */
  1839. static inline void
  1840. hal_srng_access_end(void *hal_soc, hal_ring_handle_t hal_ring_hdl)
  1841. {
  1842. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1843. if (qdf_unlikely(!hal_ring_hdl)) {
  1844. qdf_print("Error: Invalid hal_ring\n");
  1845. return;
  1846. }
  1847. hal_srng_access_end_unlocked(hal_soc, hal_ring_hdl);
  1848. SRNG_UNLOCK(&(srng->lock));
  1849. }
  1850. /* hal_srng_access_end already handles endianness conversion, so use the same */
  1851. #define hal_le_srng_access_end_in_cpu_order \
  1852. hal_srng_access_end
  1853. /**
  1854. * hal_srng_access_end_reap - Unlock ring access
  1855. * This should be used only if hal_srng_access_start to start ring access
  1856. * and should be used only while reaping SRC ring completions
  1857. *
  1858. * @hal_soc: Opaque HAL SOC handle
  1859. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  1860. *
  1861. * Return: 0 on success; error on failire
  1862. */
  1863. static inline void
  1864. hal_srng_access_end_reap(void *hal_soc, hal_ring_handle_t hal_ring_hdl)
  1865. {
  1866. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  1867. SRNG_UNLOCK(&(srng->lock));
  1868. }
  1869. /* TODO: Check if the following definitions is available in HW headers */
  1870. #define WBM_IDLE_SCATTER_BUF_SIZE 32704
  1871. #define NUM_MPDUS_PER_LINK_DESC 6
  1872. #define NUM_MSDUS_PER_LINK_DESC 7
  1873. #define REO_QUEUE_DESC_ALIGN 128
  1874. #define LINK_DESC_ALIGN 128
  1875. #define ADDRESS_MATCH_TAG_VAL 0x5
  1876. /* Number of mpdu link pointers is 9 in case of TX_MPDU_QUEUE_HEAD and 14 in
  1877. * of TX_MPDU_QUEUE_EXT. We are defining a common average count here
  1878. */
  1879. #define NUM_MPDU_LINKS_PER_QUEUE_DESC 12
  1880. /* TODO: Check with HW team on the scatter buffer size supported. As per WBM
  1881. * MLD, scatter_buffer_size in IDLE_LIST_CONTROL register is 9 bits and size
  1882. * should be specified in 16 word units. But the number of bits defined for
  1883. * this field in HW header files is 5.
  1884. */
  1885. #define WBM_IDLE_SCATTER_BUF_NEXT_PTR_SIZE 8
  1886. /**
  1887. * hal_idle_list_scatter_buf_size - Get the size of each scatter buffer
  1888. * in an idle list
  1889. *
  1890. * @hal_soc: Opaque HAL SOC handle
  1891. *
  1892. */
  1893. static inline
  1894. uint32_t hal_idle_list_scatter_buf_size(hal_soc_handle_t hal_soc_hdl)
  1895. {
  1896. return WBM_IDLE_SCATTER_BUF_SIZE;
  1897. }
  1898. /**
  1899. * hal_get_link_desc_size - Get the size of each link descriptor
  1900. *
  1901. * @hal_soc: Opaque HAL SOC handle
  1902. *
  1903. */
  1904. static inline uint32_t hal_get_link_desc_size(hal_soc_handle_t hal_soc_hdl)
  1905. {
  1906. struct hal_soc *hal_soc = (struct hal_soc *)hal_soc_hdl;
  1907. if (!hal_soc || !hal_soc->ops) {
  1908. qdf_print("Error: Invalid ops\n");
  1909. QDF_BUG(0);
  1910. return -EINVAL;
  1911. }
  1912. if (!hal_soc->ops->hal_get_link_desc_size) {
  1913. qdf_print("Error: Invalid function pointer\n");
  1914. QDF_BUG(0);
  1915. return -EINVAL;
  1916. }
  1917. return hal_soc->ops->hal_get_link_desc_size();
  1918. }
  1919. /**
  1920. * hal_get_link_desc_align - Get the required start address alignment for
  1921. * link descriptors
  1922. *
  1923. * @hal_soc: Opaque HAL SOC handle
  1924. *
  1925. */
  1926. static inline
  1927. uint32_t hal_get_link_desc_align(hal_soc_handle_t hal_soc_hdl)
  1928. {
  1929. return LINK_DESC_ALIGN;
  1930. }
  1931. /**
  1932. * hal_num_mpdus_per_link_desc - Get number of mpdus each link desc can hold
  1933. *
  1934. * @hal_soc: Opaque HAL SOC handle
  1935. *
  1936. */
  1937. static inline
  1938. uint32_t hal_num_mpdus_per_link_desc(hal_soc_handle_t hal_soc_hdl)
  1939. {
  1940. return NUM_MPDUS_PER_LINK_DESC;
  1941. }
  1942. /**
  1943. * hal_num_msdus_per_link_desc - Get number of msdus each link desc can hold
  1944. *
  1945. * @hal_soc: Opaque HAL SOC handle
  1946. *
  1947. */
  1948. static inline
  1949. uint32_t hal_num_msdus_per_link_desc(hal_soc_handle_t hal_soc_hdl)
  1950. {
  1951. return NUM_MSDUS_PER_LINK_DESC;
  1952. }
  1953. /**
  1954. * hal_num_mpdu_links_per_queue_desc - Get number of mpdu links each queue
  1955. * descriptor can hold
  1956. *
  1957. * @hal_soc: Opaque HAL SOC handle
  1958. *
  1959. */
  1960. static inline
  1961. uint32_t hal_num_mpdu_links_per_queue_desc(hal_soc_handle_t hal_soc_hdl)
  1962. {
  1963. return NUM_MPDU_LINKS_PER_QUEUE_DESC;
  1964. }
  1965. /**
  1966. * hal_idle_list_scatter_buf_num_entries - Get the number of link desc entries
  1967. * that the given buffer size
  1968. *
  1969. * @hal_soc: Opaque HAL SOC handle
  1970. * @scatter_buf_size: Size of scatter buffer
  1971. *
  1972. */
  1973. static inline
  1974. uint32_t hal_idle_scatter_buf_num_entries(hal_soc_handle_t hal_soc_hdl,
  1975. uint32_t scatter_buf_size)
  1976. {
  1977. return (scatter_buf_size - WBM_IDLE_SCATTER_BUF_NEXT_PTR_SIZE) /
  1978. hal_srng_get_entrysize(hal_soc_hdl, WBM_IDLE_LINK);
  1979. }
  1980. /**
  1981. * hal_idle_list_num_scatter_bufs - Get the number of sctater buffer
  1982. * each given buffer size
  1983. *
  1984. * @hal_soc: Opaque HAL SOC handle
  1985. * @total_mem: size of memory to be scattered
  1986. * @scatter_buf_size: Size of scatter buffer
  1987. *
  1988. */
  1989. static inline
  1990. uint32_t hal_idle_list_num_scatter_bufs(hal_soc_handle_t hal_soc_hdl,
  1991. uint32_t total_mem,
  1992. uint32_t scatter_buf_size)
  1993. {
  1994. uint8_t rem = (total_mem % (scatter_buf_size -
  1995. WBM_IDLE_SCATTER_BUF_NEXT_PTR_SIZE)) ? 1 : 0;
  1996. uint32_t num_scatter_bufs = (total_mem / (scatter_buf_size -
  1997. WBM_IDLE_SCATTER_BUF_NEXT_PTR_SIZE)) + rem;
  1998. return num_scatter_bufs;
  1999. }
  2000. enum hal_pn_type {
  2001. HAL_PN_NONE,
  2002. HAL_PN_WPA,
  2003. HAL_PN_WAPI_EVEN,
  2004. HAL_PN_WAPI_UNEVEN,
  2005. };
  2006. #define HAL_RX_MAX_BA_WINDOW 256
  2007. /**
  2008. * hal_get_reo_qdesc_align - Get start address alignment for reo
  2009. * queue descriptors
  2010. *
  2011. * @hal_soc: Opaque HAL SOC handle
  2012. *
  2013. */
  2014. static inline
  2015. uint32_t hal_get_reo_qdesc_align(hal_soc_handle_t hal_soc_hdl)
  2016. {
  2017. return REO_QUEUE_DESC_ALIGN;
  2018. }
  2019. /**
  2020. * hal_reo_qdesc_setup - Setup HW REO queue descriptor
  2021. *
  2022. * @hal_soc: Opaque HAL SOC handle
  2023. * @ba_window_size: BlockAck window size
  2024. * @start_seq: Starting sequence number
  2025. * @hw_qdesc_vaddr: Virtual address of REO queue descriptor memory
  2026. * @hw_qdesc_paddr: Physical address of REO queue descriptor memory
  2027. * @pn_type: PN type (one of the types defined in 'enum hal_pn_type')
  2028. *
  2029. */
  2030. void hal_reo_qdesc_setup(hal_soc_handle_t hal_soc_hdl,
  2031. int tid, uint32_t ba_window_size,
  2032. uint32_t start_seq, void *hw_qdesc_vaddr,
  2033. qdf_dma_addr_t hw_qdesc_paddr,
  2034. int pn_type);
  2035. /**
  2036. * hal_srng_get_hp_addr - Get head pointer physical address
  2037. *
  2038. * @hal_soc: Opaque HAL SOC handle
  2039. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  2040. *
  2041. */
  2042. static inline qdf_dma_addr_t
  2043. hal_srng_get_hp_addr(void *hal_soc,
  2044. hal_ring_handle_t hal_ring_hdl)
  2045. {
  2046. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2047. struct hal_soc *hal = (struct hal_soc *)hal_soc;
  2048. if (srng->ring_dir == HAL_SRNG_SRC_RING) {
  2049. return hal->shadow_wrptr_mem_paddr +
  2050. ((unsigned long)(srng->u.src_ring.hp_addr) -
  2051. (unsigned long)(hal->shadow_wrptr_mem_vaddr));
  2052. } else {
  2053. return hal->shadow_rdptr_mem_paddr +
  2054. ((unsigned long)(srng->u.dst_ring.hp_addr) -
  2055. (unsigned long)(hal->shadow_rdptr_mem_vaddr));
  2056. }
  2057. }
  2058. /**
  2059. * hal_srng_get_tp_addr - Get tail pointer physical address
  2060. *
  2061. * @hal_soc: Opaque HAL SOC handle
  2062. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  2063. *
  2064. */
  2065. static inline qdf_dma_addr_t
  2066. hal_srng_get_tp_addr(void *hal_soc, hal_ring_handle_t hal_ring_hdl)
  2067. {
  2068. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2069. struct hal_soc *hal = (struct hal_soc *)hal_soc;
  2070. if (srng->ring_dir == HAL_SRNG_SRC_RING) {
  2071. return hal->shadow_rdptr_mem_paddr +
  2072. ((unsigned long)(srng->u.src_ring.tp_addr) -
  2073. (unsigned long)(hal->shadow_rdptr_mem_vaddr));
  2074. } else {
  2075. return hal->shadow_wrptr_mem_paddr +
  2076. ((unsigned long)(srng->u.dst_ring.tp_addr) -
  2077. (unsigned long)(hal->shadow_wrptr_mem_vaddr));
  2078. }
  2079. }
  2080. /**
  2081. * hal_srng_get_num_entries - Get total entries in the HAL Srng
  2082. *
  2083. * @hal_soc: Opaque HAL SOC handle
  2084. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  2085. *
  2086. * Return: total number of entries in hal ring
  2087. */
  2088. static inline
  2089. uint32_t hal_srng_get_num_entries(hal_soc_handle_t hal_soc_hdl,
  2090. hal_ring_handle_t hal_ring_hdl)
  2091. {
  2092. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2093. return srng->num_entries;
  2094. }
  2095. /**
  2096. * hal_get_srng_params - Retrieve SRNG parameters for a given ring from HAL
  2097. *
  2098. * @hal_soc: Opaque HAL SOC handle
  2099. * @hal_ring_hdl: Ring pointer (Source or Destination ring)
  2100. * @ring_params: SRNG parameters will be returned through this structure
  2101. */
  2102. void hal_get_srng_params(hal_soc_handle_t hal_soc_hdl,
  2103. hal_ring_handle_t hal_ring_hdl,
  2104. struct hal_srng_params *ring_params);
  2105. /**
  2106. * hal_mem_info - Retrieve hal memory base address
  2107. *
  2108. * @hal_soc: Opaque HAL SOC handle
  2109. * @mem: pointer to structure to be updated with hal mem info
  2110. */
  2111. void hal_get_meminfo(hal_soc_handle_t hal_soc_hdl, struct hal_mem_info *mem);
  2112. /**
  2113. * hal_get_target_type - Return target type
  2114. *
  2115. * @hal_soc: Opaque HAL SOC handle
  2116. */
  2117. uint32_t hal_get_target_type(hal_soc_handle_t hal_soc_hdl);
  2118. /**
  2119. * hal_get_ba_aging_timeout - Retrieve BA aging timeout
  2120. *
  2121. * @hal_soc: Opaque HAL SOC handle
  2122. * @ac: Access category
  2123. * @value: timeout duration in millisec
  2124. */
  2125. void hal_get_ba_aging_timeout(hal_soc_handle_t hal_soc_hdl, uint8_t ac,
  2126. uint32_t *value);
  2127. /**
  2128. * hal_set_aging_timeout - Set BA aging timeout
  2129. *
  2130. * @hal_soc: Opaque HAL SOC handle
  2131. * @ac: Access category in millisec
  2132. * @value: timeout duration value
  2133. */
  2134. void hal_set_ba_aging_timeout(hal_soc_handle_t hal_soc_hdl, uint8_t ac,
  2135. uint32_t value);
  2136. /**
  2137. * hal_srng_dst_hw_init - Private function to initialize SRNG
  2138. * destination ring HW
  2139. * @hal_soc: HAL SOC handle
  2140. * @srng: SRNG ring pointer
  2141. */
  2142. static inline void hal_srng_dst_hw_init(struct hal_soc *hal,
  2143. struct hal_srng *srng)
  2144. {
  2145. hal->ops->hal_srng_dst_hw_init(hal, srng);
  2146. }
  2147. /**
  2148. * hal_srng_src_hw_init - Private function to initialize SRNG
  2149. * source ring HW
  2150. * @hal_soc: HAL SOC handle
  2151. * @srng: SRNG ring pointer
  2152. */
  2153. static inline void hal_srng_src_hw_init(struct hal_soc *hal,
  2154. struct hal_srng *srng)
  2155. {
  2156. hal->ops->hal_srng_src_hw_init(hal, srng);
  2157. }
  2158. /**
  2159. * hal_get_hw_hptp() - Get HW head and tail pointer value for any ring
  2160. * @hal_soc: Opaque HAL SOC handle
  2161. * @hal_ring_hdl: Source ring pointer
  2162. * @headp: Head Pointer
  2163. * @tailp: Tail Pointer
  2164. * @ring_type: Ring
  2165. *
  2166. * Return: Update tail pointer and head pointer in arguments.
  2167. */
  2168. static inline
  2169. void hal_get_hw_hptp(hal_soc_handle_t hal_soc_hdl,
  2170. hal_ring_handle_t hal_ring_hdl,
  2171. uint32_t *headp, uint32_t *tailp,
  2172. uint8_t ring_type)
  2173. {
  2174. struct hal_soc *hal_soc = (struct hal_soc *)hal_soc_hdl;
  2175. hal_soc->ops->hal_get_hw_hptp(hal_soc, hal_ring_hdl,
  2176. headp, tailp, ring_type);
  2177. }
  2178. /**
  2179. * hal_reo_setup - Initialize HW REO block
  2180. *
  2181. * @hal_soc: Opaque HAL SOC handle
  2182. * @reo_params: parameters needed by HAL for REO config
  2183. */
  2184. static inline void hal_reo_setup(hal_soc_handle_t hal_soc_hdl,
  2185. void *reoparams)
  2186. {
  2187. struct hal_soc *hal_soc = (struct hal_soc *)hal_soc_hdl;
  2188. hal_soc->ops->hal_reo_setup(hal_soc, reoparams);
  2189. }
  2190. static inline
  2191. void hal_compute_reo_remap_ix2_ix3(hal_soc_handle_t hal_soc_hdl,
  2192. uint32_t *ring, uint32_t num_rings,
  2193. uint32_t *remap1, uint32_t *remap2)
  2194. {
  2195. struct hal_soc *hal_soc = (struct hal_soc *)hal_soc_hdl;
  2196. return hal_soc->ops->hal_compute_reo_remap_ix2_ix3(ring,
  2197. num_rings, remap1, remap2);
  2198. }
  2199. /**
  2200. * hal_setup_link_idle_list - Setup scattered idle list using the
  2201. * buffer list provided
  2202. *
  2203. * @hal_soc: Opaque HAL SOC handle
  2204. * @scatter_bufs_base_paddr: Array of physical base addresses
  2205. * @scatter_bufs_base_vaddr: Array of virtual base addresses
  2206. * @num_scatter_bufs: Number of scatter buffers in the above lists
  2207. * @scatter_buf_size: Size of each scatter buffer
  2208. * @last_buf_end_offset: Offset to the last entry
  2209. * @num_entries: Total entries of all scatter bufs
  2210. *
  2211. */
  2212. static inline
  2213. void hal_setup_link_idle_list(hal_soc_handle_t hal_soc_hdl,
  2214. qdf_dma_addr_t scatter_bufs_base_paddr[],
  2215. void *scatter_bufs_base_vaddr[],
  2216. uint32_t num_scatter_bufs,
  2217. uint32_t scatter_buf_size,
  2218. uint32_t last_buf_end_offset,
  2219. uint32_t num_entries)
  2220. {
  2221. struct hal_soc *hal_soc = (struct hal_soc *)hal_soc_hdl;
  2222. hal_soc->ops->hal_setup_link_idle_list(hal_soc, scatter_bufs_base_paddr,
  2223. scatter_bufs_base_vaddr, num_scatter_bufs,
  2224. scatter_buf_size, last_buf_end_offset,
  2225. num_entries);
  2226. }
  2227. /**
  2228. * hal_srng_dump_ring_desc() - Dump ring descriptor info
  2229. *
  2230. * @hal_soc: Opaque HAL SOC handle
  2231. * @hal_ring_hdl: Source ring pointer
  2232. * @ring_desc: Opaque ring descriptor handle
  2233. */
  2234. static inline void hal_srng_dump_ring_desc(hal_soc_handle_t hal_soc_hdl,
  2235. hal_ring_handle_t hal_ring_hdl,
  2236. hal_ring_desc_t ring_desc)
  2237. {
  2238. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2239. QDF_TRACE_HEX_DUMP(QDF_MODULE_ID_DP, QDF_TRACE_LEVEL_INFO_HIGH,
  2240. ring_desc, (srng->entry_size << 2));
  2241. }
  2242. /**
  2243. * hal_srng_dump_ring() - Dump last 128 descs of the ring
  2244. *
  2245. * @hal_soc: Opaque HAL SOC handle
  2246. * @hal_ring_hdl: Source ring pointer
  2247. */
  2248. static inline void hal_srng_dump_ring(hal_soc_handle_t hal_soc_hdl,
  2249. hal_ring_handle_t hal_ring_hdl)
  2250. {
  2251. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2252. uint32_t *desc;
  2253. uint32_t tp, i;
  2254. tp = srng->u.dst_ring.tp;
  2255. for (i = 0; i < 128; i++) {
  2256. if (!tp)
  2257. tp = srng->ring_size;
  2258. desc = &srng->ring_base_vaddr[tp - srng->entry_size];
  2259. QDF_TRACE_HEX_DUMP(QDF_MODULE_ID_DP,
  2260. QDF_TRACE_LEVEL_DEBUG,
  2261. desc, (srng->entry_size << 2));
  2262. tp -= srng->entry_size;
  2263. }
  2264. }
  2265. /*
  2266. * hal_rxdma_desc_to_hal_ring_desc - API to convert rxdma ring desc
  2267. * to opaque dp_ring desc type
  2268. * @ring_desc - rxdma ring desc
  2269. *
  2270. * Return: hal_rxdma_desc_t type
  2271. */
  2272. static inline
  2273. hal_ring_desc_t hal_rxdma_desc_to_hal_ring_desc(hal_rxdma_desc_t ring_desc)
  2274. {
  2275. return (hal_ring_desc_t)ring_desc;
  2276. }
  2277. /**
  2278. * hal_srng_set_event() - Set hal_srng event
  2279. * @hal_ring_hdl: Source ring pointer
  2280. * @event: SRNG ring event
  2281. *
  2282. * Return: None
  2283. */
  2284. static inline void hal_srng_set_event(hal_ring_handle_t hal_ring_hdl, int event)
  2285. {
  2286. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2287. qdf_atomic_set_bit(event, &srng->srng_event);
  2288. }
  2289. /**
  2290. * hal_srng_clear_event() - Clear hal_srng event
  2291. * @hal_ring_hdl: Source ring pointer
  2292. * @event: SRNG ring event
  2293. *
  2294. * Return: None
  2295. */
  2296. static inline
  2297. void hal_srng_clear_event(hal_ring_handle_t hal_ring_hdl, int event)
  2298. {
  2299. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2300. qdf_atomic_clear_bit(event, &srng->srng_event);
  2301. }
  2302. /**
  2303. * hal_srng_get_clear_event() - Clear srng event and return old value
  2304. * @hal_ring_hdl: Source ring pointer
  2305. * @event: SRNG ring event
  2306. *
  2307. * Return: Return old event value
  2308. */
  2309. static inline
  2310. int hal_srng_get_clear_event(hal_ring_handle_t hal_ring_hdl, int event)
  2311. {
  2312. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2313. return qdf_atomic_test_and_clear_bit(event, &srng->srng_event);
  2314. }
  2315. /**
  2316. * hal_srng_set_flush_last_ts() - Record last flush time stamp
  2317. * @hal_ring_hdl: Source ring pointer
  2318. *
  2319. * Return: None
  2320. */
  2321. static inline void hal_srng_set_flush_last_ts(hal_ring_handle_t hal_ring_hdl)
  2322. {
  2323. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2324. srng->last_flush_ts = qdf_get_log_timestamp();
  2325. }
  2326. /**
  2327. * hal_srng_inc_flush_cnt() - Increment flush counter
  2328. * @hal_ring_hdl: Source ring pointer
  2329. *
  2330. * Return: None
  2331. */
  2332. static inline void hal_srng_inc_flush_cnt(hal_ring_handle_t hal_ring_hdl)
  2333. {
  2334. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2335. srng->flush_count++;
  2336. }
  2337. /**
  2338. * hal_rx_sw_mon_desc_info_get () - Get SW monitor desc info
  2339. *
  2340. * @hal: Core HAL soc handle
  2341. * @ring_desc: Mon dest ring descriptor
  2342. * @desc_info: Desc info to be populated
  2343. *
  2344. * Return void
  2345. */
  2346. static inline void
  2347. hal_rx_sw_mon_desc_info_get(struct hal_soc *hal,
  2348. hal_ring_desc_t ring_desc,
  2349. hal_rx_mon_desc_info_t desc_info)
  2350. {
  2351. return hal->ops->hal_rx_sw_mon_desc_info_get(ring_desc, desc_info);
  2352. }
  2353. /**
  2354. * hal_reo_set_err_dst_remap() - Set REO error destination ring remap
  2355. * register value.
  2356. *
  2357. * @hal_soc_hdl: Opaque HAL soc handle
  2358. *
  2359. * Return: None
  2360. */
  2361. static inline void hal_reo_set_err_dst_remap(hal_soc_handle_t hal_soc_hdl)
  2362. {
  2363. struct hal_soc *hal_soc = (struct hal_soc *)hal_soc_hdl;
  2364. if (hal_soc->ops->hal_reo_set_err_dst_remap)
  2365. hal_soc->ops->hal_reo_set_err_dst_remap(hal_soc);
  2366. }
  2367. #ifdef GENERIC_SHADOW_REGISTER_ACCESS_ENABLE
  2368. /**
  2369. * hal_set_one_target_reg_config() - Populate the target reg
  2370. * offset in hal_soc for one non srng related register at the
  2371. * given list index
  2372. * @hal_soc: hal handle
  2373. * @target_reg_offset: target register offset
  2374. * @list_index: index in hal list for shadow regs
  2375. *
  2376. * Return: none
  2377. */
  2378. void hal_set_one_target_reg_config(struct hal_soc *hal,
  2379. uint32_t target_reg_offset,
  2380. int list_index);
  2381. /**
  2382. * hal_set_shadow_regs() - Populate register offset for
  2383. * registers that need to be populated in list_shadow_reg_config
  2384. * in order to be sent to FW. These reg offsets will be mapped
  2385. * to shadow registers.
  2386. * @hal_soc: hal handle
  2387. *
  2388. * Return: QDF_STATUS_OK on success
  2389. */
  2390. QDF_STATUS hal_set_shadow_regs(void *hal_soc);
  2391. /**
  2392. * hal_construct_shadow_regs() - initialize the shadow registers
  2393. * for non-srng related register configs
  2394. * @hal_soc: hal handle
  2395. *
  2396. * Return: QDF_STATUS_OK on success
  2397. */
  2398. QDF_STATUS hal_construct_shadow_regs(void *hal_soc);
  2399. #else /* GENERIC_SHADOW_REGISTER_ACCESS_ENABLE */
  2400. static inline void hal_set_one_target_reg_config(
  2401. struct hal_soc *hal,
  2402. uint32_t target_reg_offset,
  2403. int list_index)
  2404. {
  2405. }
  2406. static inline QDF_STATUS hal_set_shadow_regs(void *hal_soc)
  2407. {
  2408. return QDF_STATUS_SUCCESS;
  2409. }
  2410. static inline QDF_STATUS hal_construct_shadow_regs(void *hal_soc)
  2411. {
  2412. return QDF_STATUS_SUCCESS;
  2413. }
  2414. #endif /* GENERIC_SHADOW_REGISTER_ACCESS_ENABLE */
  2415. #ifdef FEATURE_HAL_DELAYED_REG_WRITE
  2416. /**
  2417. * hal_flush_reg_write_work() - flush all writes from register write queue
  2418. * @arg: hal_soc pointer
  2419. *
  2420. * Return: None
  2421. */
  2422. void hal_flush_reg_write_work(hal_soc_handle_t hal_handle);
  2423. #else
  2424. static inline void hal_flush_reg_write_work(hal_soc_handle_t hal_handle) { }
  2425. #endif
  2426. /**
  2427. * hal_get_ring_usage - Calculate the ring usage percentage
  2428. * @hal_ring_hdl: Ring pointer
  2429. * @ring_type: Ring type
  2430. * @headp: pointer to head value
  2431. * @tailp: pointer to tail value
  2432. *
  2433. * Calculate the ring usage percentage for src and dest rings
  2434. *
  2435. * Return: Ring usage percentage
  2436. */
  2437. static inline
  2438. uint32_t hal_get_ring_usage(
  2439. hal_ring_handle_t hal_ring_hdl,
  2440. enum hal_ring_type ring_type, uint32_t *headp, uint32_t *tailp)
  2441. {
  2442. struct hal_srng *srng = (struct hal_srng *)hal_ring_hdl;
  2443. uint32_t num_avail, num_valid = 0;
  2444. uint32_t ring_usage;
  2445. if (srng->ring_dir == HAL_SRNG_SRC_RING) {
  2446. if (*tailp > *headp)
  2447. num_avail = ((*tailp - *headp) / srng->entry_size) - 1;
  2448. else
  2449. num_avail = ((srng->ring_size - *headp + *tailp) /
  2450. srng->entry_size) - 1;
  2451. if (ring_type == WBM_IDLE_LINK)
  2452. num_valid = num_avail;
  2453. else
  2454. num_valid = srng->num_entries - num_avail;
  2455. } else {
  2456. if (*headp >= *tailp)
  2457. num_valid = ((*headp - *tailp) / srng->entry_size);
  2458. else
  2459. num_valid = ((srng->ring_size - *tailp + *headp) /
  2460. srng->entry_size);
  2461. }
  2462. ring_usage = (100 * num_valid) / srng->num_entries;
  2463. return ring_usage;
  2464. }
  2465. #endif /* _HAL_APIH_ */