wmi_unified.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  1. /*
  2. * Copyright (c) 2015-2018 The Linux Foundation. All rights reserved.
  3. *
  4. * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
  5. *
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for
  8. * any purpose with or without fee is hereby granted, provided that the
  9. * above copyright notice and this permission notice appear in all
  10. * copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  13. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  15. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  16. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  17. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  18. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  19. * PERFORMANCE OF THIS SOFTWARE.
  20. */
  21. /*
  22. * This file was originally distributed by Qualcomm Atheros, Inc.
  23. * under proprietary terms before Copyright ownership was assigned
  24. * to the Linux Foundation.
  25. */
  26. /*
  27. * Host WMI unified implementation
  28. */
  29. #include "htc_api.h"
  30. #include "htc_api.h"
  31. #include "wmi_unified_priv.h"
  32. #ifndef WMI_NON_TLV_SUPPORT
  33. #include "wmi_tlv_helper.h"
  34. #endif
  35. #include <linux/debugfs.h>
  36. /* This check for CONFIG_WIN temporary added due to redeclaration compilation
  37. error in MCL. Error is caused due to inclusion of wmi.h in wmi_unified_api.h
  38. which gets included here through ol_if_athvar.h. Eventually it is expected that
  39. wmi.h will be removed from wmi_unified_api.h after cleanup, which will need
  40. WMI_CMD_HDR to be defined here. */
  41. #ifdef CONFIG_WIN
  42. /* Copied from wmi.h */
  43. #undef MS
  44. #define MS(_v, _f) (((_v) & _f##_MASK) >> _f##_LSB)
  45. #undef SM
  46. #define SM(_v, _f) (((_v) << _f##_LSB) & _f##_MASK)
  47. #undef WO
  48. #define WO(_f) ((_f##_OFFSET) >> 2)
  49. #undef GET_FIELD
  50. #define GET_FIELD(_addr, _f) MS(*((A_UINT32 *)(_addr) + WO(_f)), _f)
  51. #undef SET_FIELD
  52. #define SET_FIELD(_addr, _f, _val) \
  53. (*((A_UINT32 *)(_addr) + WO(_f)) = \
  54. (*((A_UINT32 *)(_addr) + WO(_f)) & ~_f##_MASK) | SM(_val, _f))
  55. #define WMI_GET_FIELD(_msg_buf, _msg_type, _f) \
  56. GET_FIELD(_msg_buf, _msg_type ## _ ## _f)
  57. #define WMI_SET_FIELD(_msg_buf, _msg_type, _f, _val) \
  58. SET_FIELD(_msg_buf, _msg_type ## _ ## _f, _val)
  59. #define WMI_EP_APASS 0x0
  60. #define WMI_EP_LPASS 0x1
  61. #define WMI_EP_SENSOR 0x2
  62. /*
  63. * * Control Path
  64. * */
  65. typedef PREPACK struct {
  66. A_UINT32 commandId:24,
  67. reserved:2, /* used for WMI endpoint ID */
  68. plt_priv:6; /* platform private */
  69. } POSTPACK WMI_CMD_HDR; /* used for commands and events */
  70. #define WMI_CMD_HDR_COMMANDID_LSB 0
  71. #define WMI_CMD_HDR_COMMANDID_MASK 0x00ffffff
  72. #define WMI_CMD_HDR_COMMANDID_OFFSET 0x00000000
  73. #define WMI_CMD_HDR_WMI_ENDPOINTID_MASK 0x03000000
  74. #define WMI_CMD_HDR_WMI_ENDPOINTID_OFFSET 24
  75. #define WMI_CMD_HDR_PLT_PRIV_LSB 24
  76. #define WMI_CMD_HDR_PLT_PRIV_MASK 0xff000000
  77. #define WMI_CMD_HDR_PLT_PRIV_OFFSET 0x00000000
  78. /* end of copy wmi.h */
  79. #endif /* CONFIG_WIN */
  80. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0))
  81. /* TODO Cleanup this backported function */
  82. static int wmi_bp_seq_printf(struct seq_file *m, const char *f, ...)
  83. {
  84. va_list args;
  85. va_start(args, f);
  86. seq_vprintf(m, f, args);
  87. va_end(args);
  88. return 0;
  89. }
  90. #else
  91. #define wmi_bp_seq_printf(m, fmt, ...) seq_printf((m), fmt, ##__VA_ARGS__)
  92. #endif
  93. #define WMI_MIN_HEAD_ROOM 64
  94. #ifdef WMI_INTERFACE_EVENT_LOGGING
  95. #ifndef MAX_WMI_INSTANCES
  96. #ifdef CONFIG_MCL
  97. #define MAX_WMI_INSTANCES 1
  98. #else
  99. #define MAX_WMI_INSTANCES 3
  100. #endif
  101. #define CUSTOM_MGMT_CMD_DATA_SIZE 4
  102. #endif
  103. #ifdef CONFIG_MCL
  104. /* WMI commands */
  105. uint32_t g_wmi_command_buf_idx = 0;
  106. struct wmi_command_debug wmi_command_log_buffer[WMI_EVENT_DEBUG_MAX_ENTRY];
  107. /* WMI commands TX completed */
  108. uint32_t g_wmi_command_tx_cmp_buf_idx = 0;
  109. struct wmi_command_debug
  110. wmi_command_tx_cmp_log_buffer[WMI_EVENT_DEBUG_MAX_ENTRY];
  111. /* WMI events when processed */
  112. uint32_t g_wmi_event_buf_idx = 0;
  113. struct wmi_event_debug wmi_event_log_buffer[WMI_EVENT_DEBUG_MAX_ENTRY];
  114. /* WMI events when queued */
  115. uint32_t g_wmi_rx_event_buf_idx = 0;
  116. struct wmi_event_debug wmi_rx_event_log_buffer[WMI_EVENT_DEBUG_MAX_ENTRY];
  117. #endif
  118. #define WMI_COMMAND_RECORD(h, a, b) { \
  119. if (wmi_log_max_entry <= \
  120. *(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx)) \
  121. *(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx) = 0;\
  122. ((struct wmi_command_debug *)h->log_info.wmi_command_log_buf_info.buf)\
  123. [*(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx)]\
  124. .command = a; \
  125. qdf_mem_copy(((struct wmi_command_debug *)h->log_info. \
  126. wmi_command_log_buf_info.buf) \
  127. [*(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx)].data,\
  128. b, wmi_record_max_length); \
  129. ((struct wmi_command_debug *)h->log_info.wmi_command_log_buf_info.buf)\
  130. [*(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx)].\
  131. time = qdf_get_log_timestamp(); \
  132. (*(h->log_info.wmi_command_log_buf_info.p_buf_tail_idx))++; \
  133. h->log_info.wmi_command_log_buf_info.length++; \
  134. }
  135. #define WMI_COMMAND_TX_CMP_RECORD(h, a, b) { \
  136. if (wmi_log_max_entry <= \
  137. *(h->log_info.wmi_command_tx_cmp_log_buf_info.p_buf_tail_idx))\
  138. *(h->log_info.wmi_command_tx_cmp_log_buf_info. \
  139. p_buf_tail_idx) = 0; \
  140. ((struct wmi_command_debug *)h->log_info. \
  141. wmi_command_tx_cmp_log_buf_info.buf) \
  142. [*(h->log_info.wmi_command_tx_cmp_log_buf_info. \
  143. p_buf_tail_idx)]. \
  144. command = a; \
  145. qdf_mem_copy(((struct wmi_command_debug *)h->log_info. \
  146. wmi_command_tx_cmp_log_buf_info.buf) \
  147. [*(h->log_info.wmi_command_tx_cmp_log_buf_info. \
  148. p_buf_tail_idx)]. \
  149. data, b, wmi_record_max_length); \
  150. ((struct wmi_command_debug *)h->log_info. \
  151. wmi_command_tx_cmp_log_buf_info.buf) \
  152. [*(h->log_info.wmi_command_tx_cmp_log_buf_info. \
  153. p_buf_tail_idx)]. \
  154. time = qdf_get_log_timestamp(); \
  155. (*(h->log_info.wmi_command_tx_cmp_log_buf_info.p_buf_tail_idx))++;\
  156. h->log_info.wmi_command_tx_cmp_log_buf_info.length++; \
  157. }
  158. #define WMI_EVENT_RECORD(h, a, b) { \
  159. if (wmi_log_max_entry <= \
  160. *(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx)) \
  161. *(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx) = 0;\
  162. ((struct wmi_event_debug *)h->log_info.wmi_event_log_buf_info.buf)\
  163. [*(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx)]. \
  164. event = a; \
  165. qdf_mem_copy(((struct wmi_event_debug *)h->log_info. \
  166. wmi_event_log_buf_info.buf) \
  167. [*(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx)].data, b,\
  168. wmi_record_max_length); \
  169. ((struct wmi_event_debug *)h->log_info.wmi_event_log_buf_info.buf)\
  170. [*(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx)].time =\
  171. qdf_get_log_timestamp(); \
  172. (*(h->log_info.wmi_event_log_buf_info.p_buf_tail_idx))++; \
  173. h->log_info.wmi_event_log_buf_info.length++; \
  174. }
  175. #define WMI_RX_EVENT_RECORD(h, a, b) { \
  176. if (wmi_log_max_entry <= \
  177. *(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx))\
  178. *(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx) = 0;\
  179. ((struct wmi_event_debug *)h->log_info.wmi_rx_event_log_buf_info.buf)\
  180. [*(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx)].\
  181. event = a; \
  182. qdf_mem_copy(((struct wmi_event_debug *)h->log_info. \
  183. wmi_rx_event_log_buf_info.buf) \
  184. [*(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx)].\
  185. data, b, wmi_record_max_length); \
  186. ((struct wmi_event_debug *)h->log_info.wmi_rx_event_log_buf_info.buf)\
  187. [*(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx)].\
  188. time = qdf_get_log_timestamp(); \
  189. (*(h->log_info.wmi_rx_event_log_buf_info.p_buf_tail_idx))++; \
  190. h->log_info.wmi_rx_event_log_buf_info.length++; \
  191. }
  192. #ifdef CONFIG_MCL
  193. uint32_t g_wmi_mgmt_command_buf_idx = 0;
  194. struct
  195. wmi_command_debug wmi_mgmt_command_log_buffer[WMI_MGMT_EVENT_DEBUG_MAX_ENTRY];
  196. /* wmi_mgmt commands TX completed */
  197. uint32_t g_wmi_mgmt_command_tx_cmp_buf_idx = 0;
  198. struct wmi_command_debug
  199. wmi_mgmt_command_tx_cmp_log_buffer[WMI_MGMT_EVENT_DEBUG_MAX_ENTRY];
  200. /* wmi_mgmt events when processed */
  201. uint32_t g_wmi_mgmt_event_buf_idx = 0;
  202. struct wmi_event_debug
  203. wmi_mgmt_event_log_buffer[WMI_MGMT_EVENT_DEBUG_MAX_ENTRY];
  204. #endif
  205. #define WMI_MGMT_COMMAND_RECORD(h, a, b) { \
  206. if (wmi_mgmt_log_max_entry <= \
  207. *(h->log_info.wmi_mgmt_command_log_buf_info.p_buf_tail_idx)) \
  208. *(h->log_info.wmi_mgmt_command_log_buf_info. \
  209. p_buf_tail_idx) = 0; \
  210. ((struct wmi_command_debug *)h->log_info. \
  211. wmi_mgmt_command_log_buf_info.buf) \
  212. [*(h->log_info.wmi_mgmt_command_log_buf_info.p_buf_tail_idx)].\
  213. command = a; \
  214. qdf_mem_copy(((struct wmi_command_debug *)h->log_info. \
  215. wmi_mgmt_command_log_buf_info.buf) \
  216. [*(h->log_info.wmi_mgmt_command_log_buf_info.p_buf_tail_idx)].\
  217. data, b, \
  218. wmi_record_max_length); \
  219. ((struct wmi_command_debug *)h->log_info. \
  220. wmi_mgmt_command_log_buf_info.buf) \
  221. [*(h->log_info.wmi_mgmt_command_log_buf_info.p_buf_tail_idx)].\
  222. time = qdf_get_log_timestamp(); \
  223. (*(h->log_info.wmi_mgmt_command_log_buf_info.p_buf_tail_idx))++;\
  224. h->log_info.wmi_mgmt_command_log_buf_info.length++; \
  225. }
  226. #define WMI_MGMT_COMMAND_TX_CMP_RECORD(h, a, b) { \
  227. if (wmi_mgmt_log_max_entry <= \
  228. *(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info. \
  229. p_buf_tail_idx)) \
  230. *(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info. \
  231. p_buf_tail_idx) = 0; \
  232. ((struct wmi_command_debug *)h->log_info. \
  233. wmi_mgmt_command_tx_cmp_log_buf_info.buf) \
  234. [*(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info. \
  235. p_buf_tail_idx)].command = a; \
  236. qdf_mem_copy(((struct wmi_command_debug *)h->log_info. \
  237. wmi_mgmt_command_tx_cmp_log_buf_info.buf)\
  238. [*(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info. \
  239. p_buf_tail_idx)].data, b, \
  240. wmi_record_max_length); \
  241. ((struct wmi_command_debug *)h->log_info. \
  242. wmi_mgmt_command_tx_cmp_log_buf_info.buf) \
  243. [*(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info. \
  244. p_buf_tail_idx)].time = \
  245. qdf_get_log_timestamp(); \
  246. (*(h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info. \
  247. p_buf_tail_idx))++; \
  248. h->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.length++; \
  249. }
  250. #define WMI_MGMT_EVENT_RECORD(h, a, b) { \
  251. if (wmi_mgmt_log_max_entry <= \
  252. *(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx))\
  253. *(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx) = 0;\
  254. ((struct wmi_event_debug *)h->log_info.wmi_mgmt_event_log_buf_info.buf)\
  255. [*(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx)]\
  256. .event = a; \
  257. qdf_mem_copy(((struct wmi_event_debug *)h->log_info. \
  258. wmi_mgmt_event_log_buf_info.buf) \
  259. [*(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx)].\
  260. data, b, wmi_record_max_length); \
  261. ((struct wmi_event_debug *)h->log_info.wmi_mgmt_event_log_buf_info.buf)\
  262. [*(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx)].\
  263. time = qdf_get_log_timestamp(); \
  264. (*(h->log_info.wmi_mgmt_event_log_buf_info.p_buf_tail_idx))++; \
  265. h->log_info.wmi_mgmt_event_log_buf_info.length++; \
  266. }
  267. /* These are defined to made it as module param, which can be configured */
  268. uint32_t wmi_log_max_entry = WMI_EVENT_DEBUG_MAX_ENTRY;
  269. uint32_t wmi_mgmt_log_max_entry = WMI_MGMT_EVENT_DEBUG_MAX_ENTRY;
  270. uint32_t wmi_record_max_length = WMI_EVENT_DEBUG_ENTRY_MAX_LENGTH;
  271. uint32_t wmi_display_size = 100;
  272. /**
  273. * wmi_log_init() - Initialize WMI event logging
  274. * @wmi_handle: WMI handle.
  275. *
  276. * Return: Initialization status
  277. */
  278. #ifdef CONFIG_MCL
  279. static QDF_STATUS wmi_log_init(struct wmi_unified *wmi_handle)
  280. {
  281. struct wmi_log_buf_t *cmd_log_buf =
  282. &wmi_handle->log_info.wmi_command_log_buf_info;
  283. struct wmi_log_buf_t *cmd_tx_cmpl_log_buf =
  284. &wmi_handle->log_info.wmi_command_tx_cmp_log_buf_info;
  285. struct wmi_log_buf_t *event_log_buf =
  286. &wmi_handle->log_info.wmi_event_log_buf_info;
  287. struct wmi_log_buf_t *rx_event_log_buf =
  288. &wmi_handle->log_info.wmi_rx_event_log_buf_info;
  289. struct wmi_log_buf_t *mgmt_cmd_log_buf =
  290. &wmi_handle->log_info.wmi_mgmt_command_log_buf_info;
  291. struct wmi_log_buf_t *mgmt_cmd_tx_cmp_log_buf =
  292. &wmi_handle->log_info.wmi_mgmt_command_tx_cmp_log_buf_info;
  293. struct wmi_log_buf_t *mgmt_event_log_buf =
  294. &wmi_handle->log_info.wmi_mgmt_event_log_buf_info;
  295. /* WMI commands */
  296. cmd_log_buf->length = 0;
  297. cmd_log_buf->buf_tail_idx = 0;
  298. cmd_log_buf->buf = wmi_command_log_buffer;
  299. cmd_log_buf->p_buf_tail_idx = &g_wmi_command_buf_idx;
  300. cmd_log_buf->size = WMI_EVENT_DEBUG_MAX_ENTRY;
  301. /* WMI commands TX completed */
  302. cmd_tx_cmpl_log_buf->length = 0;
  303. cmd_tx_cmpl_log_buf->buf_tail_idx = 0;
  304. cmd_tx_cmpl_log_buf->buf = wmi_command_tx_cmp_log_buffer;
  305. cmd_tx_cmpl_log_buf->p_buf_tail_idx = &g_wmi_command_tx_cmp_buf_idx;
  306. cmd_tx_cmpl_log_buf->size = WMI_EVENT_DEBUG_MAX_ENTRY;
  307. /* WMI events when processed */
  308. event_log_buf->length = 0;
  309. event_log_buf->buf_tail_idx = 0;
  310. event_log_buf->buf = wmi_event_log_buffer;
  311. event_log_buf->p_buf_tail_idx = &g_wmi_event_buf_idx;
  312. event_log_buf->size = WMI_EVENT_DEBUG_MAX_ENTRY;
  313. /* WMI events when queued */
  314. rx_event_log_buf->length = 0;
  315. rx_event_log_buf->buf_tail_idx = 0;
  316. rx_event_log_buf->buf = wmi_rx_event_log_buffer;
  317. rx_event_log_buf->p_buf_tail_idx = &g_wmi_rx_event_buf_idx;
  318. rx_event_log_buf->size = WMI_EVENT_DEBUG_MAX_ENTRY;
  319. /* WMI Management commands */
  320. mgmt_cmd_log_buf->length = 0;
  321. mgmt_cmd_log_buf->buf_tail_idx = 0;
  322. mgmt_cmd_log_buf->buf = wmi_mgmt_command_log_buffer;
  323. mgmt_cmd_log_buf->p_buf_tail_idx = &g_wmi_mgmt_command_buf_idx;
  324. mgmt_cmd_log_buf->size = WMI_MGMT_EVENT_DEBUG_MAX_ENTRY;
  325. /* WMI Management commands Tx completed*/
  326. mgmt_cmd_tx_cmp_log_buf->length = 0;
  327. mgmt_cmd_tx_cmp_log_buf->buf_tail_idx = 0;
  328. mgmt_cmd_tx_cmp_log_buf->buf = wmi_mgmt_command_tx_cmp_log_buffer;
  329. mgmt_cmd_tx_cmp_log_buf->p_buf_tail_idx =
  330. &g_wmi_mgmt_command_tx_cmp_buf_idx;
  331. mgmt_cmd_tx_cmp_log_buf->size = WMI_MGMT_EVENT_DEBUG_MAX_ENTRY;
  332. /* WMI Management events when processed*/
  333. mgmt_event_log_buf->length = 0;
  334. mgmt_event_log_buf->buf_tail_idx = 0;
  335. mgmt_event_log_buf->buf = wmi_mgmt_event_log_buffer;
  336. mgmt_event_log_buf->p_buf_tail_idx = &g_wmi_mgmt_event_buf_idx;
  337. mgmt_event_log_buf->size = WMI_MGMT_EVENT_DEBUG_MAX_ENTRY;
  338. qdf_spinlock_create(&wmi_handle->log_info.wmi_record_lock);
  339. wmi_handle->log_info.wmi_logging_enable = 1;
  340. return QDF_STATUS_SUCCESS;
  341. }
  342. #else
  343. static QDF_STATUS wmi_log_init(struct wmi_unified *wmi_handle)
  344. {
  345. struct wmi_log_buf_t *cmd_log_buf =
  346. &wmi_handle->log_info.wmi_command_log_buf_info;
  347. struct wmi_log_buf_t *cmd_tx_cmpl_log_buf =
  348. &wmi_handle->log_info.wmi_command_tx_cmp_log_buf_info;
  349. struct wmi_log_buf_t *event_log_buf =
  350. &wmi_handle->log_info.wmi_event_log_buf_info;
  351. struct wmi_log_buf_t *rx_event_log_buf =
  352. &wmi_handle->log_info.wmi_rx_event_log_buf_info;
  353. struct wmi_log_buf_t *mgmt_cmd_log_buf =
  354. &wmi_handle->log_info.wmi_mgmt_command_log_buf_info;
  355. struct wmi_log_buf_t *mgmt_cmd_tx_cmp_log_buf =
  356. &wmi_handle->log_info.wmi_mgmt_command_tx_cmp_log_buf_info;
  357. struct wmi_log_buf_t *mgmt_event_log_buf =
  358. &wmi_handle->log_info.wmi_mgmt_event_log_buf_info;
  359. wmi_handle->log_info.wmi_logging_enable = 0;
  360. /* WMI commands */
  361. cmd_log_buf->length = 0;
  362. cmd_log_buf->buf_tail_idx = 0;
  363. cmd_log_buf->buf = (struct wmi_command_debug *) qdf_mem_malloc(
  364. wmi_log_max_entry * sizeof(struct wmi_command_debug));
  365. cmd_log_buf->size = wmi_log_max_entry;
  366. if (!cmd_log_buf->buf) {
  367. qdf_print("no memory for WMI command log buffer..\n");
  368. return QDF_STATUS_E_NOMEM;
  369. }
  370. cmd_log_buf->p_buf_tail_idx = &cmd_log_buf->buf_tail_idx;
  371. /* WMI commands TX completed */
  372. cmd_tx_cmpl_log_buf->length = 0;
  373. cmd_tx_cmpl_log_buf->buf_tail_idx = 0;
  374. cmd_tx_cmpl_log_buf->buf = (struct wmi_command_debug *) qdf_mem_malloc(
  375. wmi_log_max_entry * sizeof(struct wmi_command_debug));
  376. cmd_tx_cmpl_log_buf->size = wmi_log_max_entry;
  377. if (!cmd_tx_cmpl_log_buf->buf) {
  378. qdf_print("no memory for WMI Command Tx Complete log buffer..\n");
  379. return QDF_STATUS_E_NOMEM;
  380. }
  381. cmd_tx_cmpl_log_buf->p_buf_tail_idx =
  382. &cmd_tx_cmpl_log_buf->buf_tail_idx;
  383. /* WMI events when processed */
  384. event_log_buf->length = 0;
  385. event_log_buf->buf_tail_idx = 0;
  386. event_log_buf->buf = (struct wmi_event_debug *) qdf_mem_malloc(
  387. wmi_log_max_entry * sizeof(struct wmi_event_debug));
  388. event_log_buf->size = wmi_log_max_entry;
  389. if (!event_log_buf->buf) {
  390. qdf_print("no memory for WMI Event log buffer..\n");
  391. return QDF_STATUS_E_NOMEM;
  392. }
  393. event_log_buf->p_buf_tail_idx = &event_log_buf->buf_tail_idx;
  394. /* WMI events when queued */
  395. rx_event_log_buf->length = 0;
  396. rx_event_log_buf->buf_tail_idx = 0;
  397. rx_event_log_buf->buf = (struct wmi_event_debug *) qdf_mem_malloc(
  398. wmi_log_max_entry * sizeof(struct wmi_event_debug));
  399. rx_event_log_buf->size = wmi_log_max_entry;
  400. if (!rx_event_log_buf->buf) {
  401. qdf_print("no memory for WMI Event Rx log buffer..\n");
  402. return QDF_STATUS_E_NOMEM;
  403. }
  404. rx_event_log_buf->p_buf_tail_idx = &rx_event_log_buf->buf_tail_idx;
  405. /* WMI Management commands */
  406. mgmt_cmd_log_buf->length = 0;
  407. mgmt_cmd_log_buf->buf_tail_idx = 0;
  408. mgmt_cmd_log_buf->buf = (struct wmi_command_debug *) qdf_mem_malloc(
  409. wmi_mgmt_log_max_entry * sizeof(struct wmi_command_debug));
  410. mgmt_cmd_log_buf->size = wmi_mgmt_log_max_entry;
  411. if (!mgmt_cmd_log_buf->buf) {
  412. qdf_print("no memory for WMI Management Command log buffer..\n");
  413. return QDF_STATUS_E_NOMEM;
  414. }
  415. mgmt_cmd_log_buf->p_buf_tail_idx = &mgmt_cmd_log_buf->buf_tail_idx;
  416. /* WMI Management commands Tx completed*/
  417. mgmt_cmd_tx_cmp_log_buf->length = 0;
  418. mgmt_cmd_tx_cmp_log_buf->buf_tail_idx = 0;
  419. mgmt_cmd_tx_cmp_log_buf->buf = (struct wmi_command_debug *)
  420. qdf_mem_malloc(
  421. wmi_mgmt_log_max_entry *
  422. sizeof(struct wmi_command_debug));
  423. mgmt_cmd_tx_cmp_log_buf->size = wmi_mgmt_log_max_entry;
  424. if (!mgmt_cmd_tx_cmp_log_buf->buf) {
  425. qdf_print("no memory for WMI Management Command Tx complete log buffer..\n");
  426. return QDF_STATUS_E_NOMEM;
  427. }
  428. mgmt_cmd_tx_cmp_log_buf->p_buf_tail_idx =
  429. &mgmt_cmd_tx_cmp_log_buf->buf_tail_idx;
  430. /* WMI Management events when processed*/
  431. mgmt_event_log_buf->length = 0;
  432. mgmt_event_log_buf->buf_tail_idx = 0;
  433. mgmt_event_log_buf->buf = (struct wmi_event_debug *) qdf_mem_malloc(
  434. wmi_mgmt_log_max_entry *
  435. sizeof(struct wmi_event_debug));
  436. mgmt_event_log_buf->size = wmi_mgmt_log_max_entry;
  437. if (!mgmt_event_log_buf->buf) {
  438. qdf_print("no memory for WMI Management Event log buffer..\n");
  439. return QDF_STATUS_E_NOMEM;
  440. }
  441. mgmt_event_log_buf->p_buf_tail_idx = &mgmt_event_log_buf->buf_tail_idx;
  442. qdf_spinlock_create(&wmi_handle->log_info.wmi_record_lock);
  443. wmi_handle->log_info.wmi_logging_enable = 1;
  444. return QDF_STATUS_SUCCESS;
  445. }
  446. #endif
  447. /**
  448. * wmi_log_buffer_free() - Free all dynamic allocated buffer memory for
  449. * event logging
  450. * @wmi_handle: WMI handle.
  451. *
  452. * Return: None
  453. */
  454. #ifndef CONFIG_MCL
  455. static inline void wmi_log_buffer_free(struct wmi_unified *wmi_handle)
  456. {
  457. if (wmi_handle->log_info.wmi_command_log_buf_info.buf)
  458. qdf_mem_free(wmi_handle->log_info.wmi_command_log_buf_info.buf);
  459. if (wmi_handle->log_info.wmi_command_tx_cmp_log_buf_info.buf)
  460. qdf_mem_free(
  461. wmi_handle->log_info.wmi_command_tx_cmp_log_buf_info.buf);
  462. if (wmi_handle->log_info.wmi_event_log_buf_info.buf)
  463. qdf_mem_free(wmi_handle->log_info.wmi_event_log_buf_info.buf);
  464. if (wmi_handle->log_info.wmi_rx_event_log_buf_info.buf)
  465. qdf_mem_free(
  466. wmi_handle->log_info.wmi_rx_event_log_buf_info.buf);
  467. if (wmi_handle->log_info.wmi_mgmt_command_log_buf_info.buf)
  468. qdf_mem_free(
  469. wmi_handle->log_info.wmi_mgmt_command_log_buf_info.buf);
  470. if (wmi_handle->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.buf)
  471. qdf_mem_free(
  472. wmi_handle->log_info.wmi_mgmt_command_tx_cmp_log_buf_info.buf);
  473. if (wmi_handle->log_info.wmi_mgmt_event_log_buf_info.buf)
  474. qdf_mem_free(
  475. wmi_handle->log_info.wmi_mgmt_event_log_buf_info.buf);
  476. wmi_handle->log_info.wmi_logging_enable = 0;
  477. qdf_spinlock_destroy(&wmi_handle->log_info.wmi_record_lock);
  478. }
  479. #else
  480. static inline void wmi_log_buffer_free(struct wmi_unified *wmi_handle)
  481. {
  482. /* Do Nothing */
  483. }
  484. #endif
  485. /**
  486. * wmi_print_cmd_log_buffer() - an output agnostic wmi command log printer
  487. * @log_buffer: the command log buffer metadata of the buffer to print
  488. * @count: the maximum number of entries to print
  489. * @print: an abstract print method, e.g. a qdf_print() or seq_printf() wrapper
  490. * @print_priv: any data required by the print method, e.g. a file handle
  491. *
  492. * Return: None
  493. */
  494. static void
  495. wmi_print_cmd_log_buffer(struct wmi_log_buf_t *log_buffer, uint32_t count,
  496. qdf_abstract_print *print, void *print_priv)
  497. {
  498. static const int data_len =
  499. WMI_EVENT_DEBUG_ENTRY_MAX_LENGTH / sizeof(uint32_t);
  500. char str[128];
  501. uint32_t idx;
  502. if (count > log_buffer->size)
  503. count = log_buffer->size;
  504. if (count > log_buffer->length)
  505. count = log_buffer->length;
  506. /* subtract count from index, and wrap if necessary */
  507. idx = log_buffer->size + *log_buffer->p_buf_tail_idx - count;
  508. idx %= log_buffer->size;
  509. print(print_priv, "Time (seconds) Cmd Id Payload");
  510. while (count) {
  511. struct wmi_command_debug *cmd_log = (struct wmi_command_debug *)
  512. &((struct wmi_command_debug *)log_buffer->buf)[idx];
  513. uint64_t secs, usecs;
  514. int len = 0;
  515. int i;
  516. qdf_log_timestamp_to_secs(cmd_log->time, &secs, &usecs);
  517. len += scnprintf(str + len, sizeof(str) - len,
  518. "% 8lld.%06lld %6u (0x%06x) ",
  519. secs, usecs,
  520. cmd_log->command, cmd_log->command);
  521. for (i = 0; i < data_len; ++i) {
  522. len += scnprintf(str + len, sizeof(str) - len,
  523. "0x%08x ", cmd_log->data[i]);
  524. }
  525. print(print_priv, str);
  526. --count;
  527. ++idx;
  528. if (idx >= log_buffer->size)
  529. idx = 0;
  530. }
  531. }
  532. /**
  533. * wmi_print_event_log_buffer() - an output agnostic wmi event log printer
  534. * @log_buffer: the event log buffer metadata of the buffer to print
  535. * @count: the maximum number of entries to print
  536. * @print: an abstract print method, e.g. a qdf_print() or seq_printf() wrapper
  537. * @print_priv: any data required by the print method, e.g. a file handle
  538. *
  539. * Return: None
  540. */
  541. static void
  542. wmi_print_event_log_buffer(struct wmi_log_buf_t *log_buffer, uint32_t count,
  543. qdf_abstract_print *print, void *print_priv)
  544. {
  545. static const int data_len =
  546. WMI_EVENT_DEBUG_ENTRY_MAX_LENGTH / sizeof(uint32_t);
  547. char str[128];
  548. uint32_t idx;
  549. if (count > log_buffer->size)
  550. count = log_buffer->size;
  551. if (count > log_buffer->length)
  552. count = log_buffer->length;
  553. /* subtract count from index, and wrap if necessary */
  554. idx = log_buffer->size + *log_buffer->p_buf_tail_idx - count;
  555. idx %= log_buffer->size;
  556. print(print_priv, "Time (seconds) Event Id Payload");
  557. while (count) {
  558. struct wmi_event_debug *event_log = (struct wmi_event_debug *)
  559. &((struct wmi_event_debug *)log_buffer->buf)[idx];
  560. uint64_t secs, usecs;
  561. int len = 0;
  562. int i;
  563. qdf_log_timestamp_to_secs(event_log->time, &secs, &usecs);
  564. len += scnprintf(str + len, sizeof(str) - len,
  565. "% 8lld.%06lld %6u (0x%06x) ",
  566. secs, usecs,
  567. event_log->event, event_log->event);
  568. for (i = 0; i < data_len; ++i) {
  569. len += scnprintf(str + len, sizeof(str) - len,
  570. "0x%08x ", event_log->data[i]);
  571. }
  572. print(print_priv, str);
  573. --count;
  574. ++idx;
  575. if (idx >= log_buffer->size)
  576. idx = 0;
  577. }
  578. }
  579. inline void
  580. wmi_print_cmd_log(wmi_unified_t wmi, uint32_t count,
  581. qdf_abstract_print *print, void *print_priv)
  582. {
  583. wmi_print_cmd_log_buffer(
  584. &wmi->log_info.wmi_command_log_buf_info,
  585. count, print, print_priv);
  586. }
  587. inline void
  588. wmi_print_cmd_tx_cmp_log(wmi_unified_t wmi, uint32_t count,
  589. qdf_abstract_print *print, void *print_priv)
  590. {
  591. wmi_print_cmd_log_buffer(
  592. &wmi->log_info.wmi_command_tx_cmp_log_buf_info,
  593. count, print, print_priv);
  594. }
  595. inline void
  596. wmi_print_mgmt_cmd_log(wmi_unified_t wmi, uint32_t count,
  597. qdf_abstract_print *print, void *print_priv)
  598. {
  599. wmi_print_cmd_log_buffer(
  600. &wmi->log_info.wmi_mgmt_command_log_buf_info,
  601. count, print, print_priv);
  602. }
  603. inline void
  604. wmi_print_mgmt_cmd_tx_cmp_log(wmi_unified_t wmi, uint32_t count,
  605. qdf_abstract_print *print, void *print_priv)
  606. {
  607. wmi_print_cmd_log_buffer(
  608. &wmi->log_info.wmi_mgmt_command_tx_cmp_log_buf_info,
  609. count, print, print_priv);
  610. }
  611. inline void
  612. wmi_print_event_log(wmi_unified_t wmi, uint32_t count,
  613. qdf_abstract_print *print, void *print_priv)
  614. {
  615. wmi_print_event_log_buffer(
  616. &wmi->log_info.wmi_event_log_buf_info,
  617. count, print, print_priv);
  618. }
  619. inline void
  620. wmi_print_rx_event_log(wmi_unified_t wmi, uint32_t count,
  621. qdf_abstract_print *print, void *print_priv)
  622. {
  623. wmi_print_event_log_buffer(
  624. &wmi->log_info.wmi_rx_event_log_buf_info,
  625. count, print, print_priv);
  626. }
  627. inline void
  628. wmi_print_mgmt_event_log(wmi_unified_t wmi, uint32_t count,
  629. qdf_abstract_print *print, void *print_priv)
  630. {
  631. wmi_print_event_log_buffer(
  632. &wmi->log_info.wmi_mgmt_event_log_buf_info,
  633. count, print, print_priv);
  634. }
  635. #ifdef CONFIG_MCL
  636. const int8_t * const debugfs_dir[MAX_WMI_INSTANCES] = {"WMI0"};
  637. #else
  638. const int8_t * const debugfs_dir[MAX_WMI_INSTANCES] = {"WMI0", "WMI1", "WMI2"};
  639. #endif
  640. /* debugfs routines*/
  641. /**
  642. * debug_wmi_##func_base##_show() - debugfs functions to display content of
  643. * command and event buffers. Macro uses max buffer length to display
  644. * buffer when it is wraparound.
  645. *
  646. * @m: debugfs handler to access wmi_handle
  647. * @v: Variable arguments (not used)
  648. *
  649. * Return: Length of characters printed
  650. */
  651. #define GENERATE_COMMAND_DEBUG_SHOW_FUNCS(func_base, wmi_ring_size) \
  652. static int debug_wmi_##func_base##_show(struct seq_file *m, \
  653. void *v) \
  654. { \
  655. wmi_unified_t wmi_handle = (wmi_unified_t) m->private; \
  656. struct wmi_log_buf_t *wmi_log = \
  657. &wmi_handle->log_info.wmi_##func_base##_buf_info;\
  658. int pos, nread, outlen; \
  659. int i; \
  660. uint64_t secs, usecs; \
  661. \
  662. qdf_spin_lock(&wmi_handle->log_info.wmi_record_lock); \
  663. if (!wmi_log->length) { \
  664. qdf_spin_unlock(&wmi_handle->log_info.wmi_record_lock);\
  665. return wmi_bp_seq_printf(m, \
  666. "no elements to read from ring buffer!\n"); \
  667. } \
  668. \
  669. if (wmi_log->length <= wmi_ring_size) \
  670. nread = wmi_log->length; \
  671. else \
  672. nread = wmi_ring_size; \
  673. \
  674. if (*(wmi_log->p_buf_tail_idx) == 0) \
  675. /* tail can be 0 after wrap-around */ \
  676. pos = wmi_ring_size - 1; \
  677. else \
  678. pos = *(wmi_log->p_buf_tail_idx) - 1; \
  679. \
  680. outlen = wmi_bp_seq_printf(m, "Length = %d\n", wmi_log->length);\
  681. qdf_spin_unlock(&wmi_handle->log_info.wmi_record_lock); \
  682. while (nread--) { \
  683. struct wmi_command_debug *wmi_record; \
  684. \
  685. wmi_record = (struct wmi_command_debug *) \
  686. &(((struct wmi_command_debug *)wmi_log->buf)[pos]);\
  687. outlen += wmi_bp_seq_printf(m, "CMD ID = %x\n", \
  688. (wmi_record->command)); \
  689. qdf_log_timestamp_to_secs(wmi_record->time, &secs,\
  690. &usecs); \
  691. outlen += \
  692. wmi_bp_seq_printf(m, "CMD TIME = [%llu.%06llu]\n",\
  693. secs, usecs); \
  694. outlen += wmi_bp_seq_printf(m, "CMD = "); \
  695. for (i = 0; i < (wmi_record_max_length/ \
  696. sizeof(uint32_t)); i++) \
  697. outlen += wmi_bp_seq_printf(m, "%x ", \
  698. wmi_record->data[i]); \
  699. outlen += wmi_bp_seq_printf(m, "\n"); \
  700. \
  701. if (pos == 0) \
  702. pos = wmi_ring_size - 1; \
  703. else \
  704. pos--; \
  705. } \
  706. return outlen; \
  707. } \
  708. #define GENERATE_EVENT_DEBUG_SHOW_FUNCS(func_base, wmi_ring_size) \
  709. static int debug_wmi_##func_base##_show(struct seq_file *m, \
  710. void *v) \
  711. { \
  712. wmi_unified_t wmi_handle = (wmi_unified_t) m->private; \
  713. struct wmi_log_buf_t *wmi_log = \
  714. &wmi_handle->log_info.wmi_##func_base##_buf_info;\
  715. int pos, nread, outlen; \
  716. int i; \
  717. uint64_t secs, usecs; \
  718. \
  719. qdf_spin_lock(&wmi_handle->log_info.wmi_record_lock); \
  720. if (!wmi_log->length) { \
  721. qdf_spin_unlock(&wmi_handle->log_info.wmi_record_lock);\
  722. return wmi_bp_seq_printf(m, \
  723. "no elements to read from ring buffer!\n"); \
  724. } \
  725. \
  726. if (wmi_log->length <= wmi_ring_size) \
  727. nread = wmi_log->length; \
  728. else \
  729. nread = wmi_ring_size; \
  730. \
  731. if (*(wmi_log->p_buf_tail_idx) == 0) \
  732. /* tail can be 0 after wrap-around */ \
  733. pos = wmi_ring_size - 1; \
  734. else \
  735. pos = *(wmi_log->p_buf_tail_idx) - 1; \
  736. \
  737. outlen = wmi_bp_seq_printf(m, "Length = %d\n", wmi_log->length);\
  738. qdf_spin_unlock(&wmi_handle->log_info.wmi_record_lock); \
  739. while (nread--) { \
  740. struct wmi_event_debug *wmi_record; \
  741. \
  742. wmi_record = (struct wmi_event_debug *) \
  743. &(((struct wmi_event_debug *)wmi_log->buf)[pos]);\
  744. qdf_log_timestamp_to_secs(wmi_record->time, &secs,\
  745. &usecs); \
  746. outlen += wmi_bp_seq_printf(m, "Event ID = %x\n",\
  747. (wmi_record->event)); \
  748. outlen += \
  749. wmi_bp_seq_printf(m, "Event TIME = [%llu.%06llu]\n",\
  750. secs, usecs); \
  751. outlen += wmi_bp_seq_printf(m, "CMD = "); \
  752. for (i = 0; i < (wmi_record_max_length/ \
  753. sizeof(uint32_t)); i++) \
  754. outlen += wmi_bp_seq_printf(m, "%x ", \
  755. wmi_record->data[i]); \
  756. outlen += wmi_bp_seq_printf(m, "\n"); \
  757. \
  758. if (pos == 0) \
  759. pos = wmi_ring_size - 1; \
  760. else \
  761. pos--; \
  762. } \
  763. return outlen; \
  764. }
  765. GENERATE_COMMAND_DEBUG_SHOW_FUNCS(command_log, wmi_display_size);
  766. GENERATE_COMMAND_DEBUG_SHOW_FUNCS(command_tx_cmp_log, wmi_display_size);
  767. GENERATE_EVENT_DEBUG_SHOW_FUNCS(event_log, wmi_display_size);
  768. GENERATE_EVENT_DEBUG_SHOW_FUNCS(rx_event_log, wmi_display_size);
  769. GENERATE_COMMAND_DEBUG_SHOW_FUNCS(mgmt_command_log, wmi_display_size);
  770. GENERATE_COMMAND_DEBUG_SHOW_FUNCS(mgmt_command_tx_cmp_log,
  771. wmi_display_size);
  772. GENERATE_EVENT_DEBUG_SHOW_FUNCS(mgmt_event_log, wmi_display_size);
  773. /**
  774. * debug_wmi_enable_show() - debugfs functions to display enable state of
  775. * wmi logging feature.
  776. *
  777. * @m: debugfs handler to access wmi_handle
  778. * @v: Variable arguments (not used)
  779. *
  780. * Return: always 1
  781. */
  782. static int debug_wmi_enable_show(struct seq_file *m, void *v)
  783. {
  784. wmi_unified_t wmi_handle = (wmi_unified_t) m->private;
  785. return wmi_bp_seq_printf(m, "%d\n",
  786. wmi_handle->log_info.wmi_logging_enable);
  787. }
  788. /**
  789. * debug_wmi_log_size_show() - debugfs functions to display configured size of
  790. * wmi logging command/event buffer and management command/event buffer.
  791. *
  792. * @m: debugfs handler to access wmi_handle
  793. * @v: Variable arguments (not used)
  794. *
  795. * Return: Length of characters printed
  796. */
  797. static int debug_wmi_log_size_show(struct seq_file *m, void *v)
  798. {
  799. wmi_bp_seq_printf(m, "WMI command/event log max size:%d\n",
  800. wmi_log_max_entry);
  801. return wmi_bp_seq_printf(m,
  802. "WMI management command/events log max size:%d\n",
  803. wmi_mgmt_log_max_entry);
  804. }
  805. /**
  806. * debug_wmi_##func_base##_write() - debugfs functions to clear
  807. * wmi logging command/event buffer and management command/event buffer.
  808. *
  809. * @file: file handler to access wmi_handle
  810. * @buf: received data buffer
  811. * @count: length of received buffer
  812. * @ppos: Not used
  813. *
  814. * Return: count
  815. */
  816. #define GENERATE_DEBUG_WRITE_FUNCS(func_base, wmi_ring_size, wmi_record_type)\
  817. static ssize_t debug_wmi_##func_base##_write(struct file *file, \
  818. const char __user *buf, \
  819. size_t count, loff_t *ppos) \
  820. { \
  821. int k, ret; \
  822. wmi_unified_t wmi_handle = \
  823. ((struct seq_file *)file->private_data)->private;\
  824. struct wmi_log_buf_t *wmi_log = &wmi_handle->log_info. \
  825. wmi_##func_base##_buf_info; \
  826. char locbuf[50]; \
  827. \
  828. if ((!buf) || (count > 50)) \
  829. return -EFAULT; \
  830. \
  831. if (copy_from_user(locbuf, buf, count)) \
  832. return -EFAULT; \
  833. \
  834. ret = sscanf(locbuf, "%d", &k); \
  835. if ((ret != 1) || (k != 0)) { \
  836. qdf_print("Wrong input, echo 0 to clear the wmi buffer\n");\
  837. return -EINVAL; \
  838. } \
  839. \
  840. qdf_spin_lock(&wmi_handle->log_info.wmi_record_lock); \
  841. qdf_mem_zero(wmi_log->buf, wmi_ring_size * \
  842. sizeof(struct wmi_record_type)); \
  843. wmi_log->length = 0; \
  844. *(wmi_log->p_buf_tail_idx) = 0; \
  845. qdf_spin_unlock(&wmi_handle->log_info.wmi_record_lock); \
  846. \
  847. return count; \
  848. }
  849. GENERATE_DEBUG_WRITE_FUNCS(command_log, wmi_log_max_entry,
  850. wmi_command_debug);
  851. GENERATE_DEBUG_WRITE_FUNCS(command_tx_cmp_log, wmi_log_max_entry,
  852. wmi_command_debug);
  853. GENERATE_DEBUG_WRITE_FUNCS(event_log, wmi_log_max_entry,
  854. wmi_event_debug);
  855. GENERATE_DEBUG_WRITE_FUNCS(rx_event_log, wmi_log_max_entry,
  856. wmi_event_debug);
  857. GENERATE_DEBUG_WRITE_FUNCS(mgmt_command_log, wmi_mgmt_log_max_entry,
  858. wmi_command_debug);
  859. GENERATE_DEBUG_WRITE_FUNCS(mgmt_command_tx_cmp_log,
  860. wmi_mgmt_log_max_entry, wmi_command_debug);
  861. GENERATE_DEBUG_WRITE_FUNCS(mgmt_event_log, wmi_mgmt_log_max_entry,
  862. wmi_event_debug);
  863. /**
  864. * debug_wmi_enable_write() - debugfs functions to enable/disable
  865. * wmi logging feature.
  866. *
  867. * @file: file handler to access wmi_handle
  868. * @buf: received data buffer
  869. * @count: length of received buffer
  870. * @ppos: Not used
  871. *
  872. * Return: count
  873. */
  874. static ssize_t debug_wmi_enable_write(struct file *file, const char __user *buf,
  875. size_t count, loff_t *ppos)
  876. {
  877. wmi_unified_t wmi_handle =
  878. ((struct seq_file *)file->private_data)->private;
  879. int k, ret;
  880. char locbuf[50];
  881. if ((!buf) || (count > 50))
  882. return -EFAULT;
  883. if (copy_from_user(locbuf, buf, count))
  884. return -EFAULT;
  885. ret = sscanf(locbuf, "%d", &k);
  886. if ((ret != 1) || ((k != 0) && (k != 1)))
  887. return -EINVAL;
  888. wmi_handle->log_info.wmi_logging_enable = k;
  889. return count;
  890. }
  891. /**
  892. * debug_wmi_log_size_write() - reserved.
  893. *
  894. * @file: file handler to access wmi_handle
  895. * @buf: received data buffer
  896. * @count: length of received buffer
  897. * @ppos: Not used
  898. *
  899. * Return: count
  900. */
  901. static ssize_t debug_wmi_log_size_write(struct file *file,
  902. const char __user *buf, size_t count, loff_t *ppos)
  903. {
  904. return -EINVAL;
  905. }
  906. /* Structure to maintain debug information */
  907. struct wmi_debugfs_info {
  908. const char *name;
  909. struct dentry *de[MAX_WMI_INSTANCES];
  910. const struct file_operations *ops;
  911. };
  912. #define DEBUG_FOO(func_base) { .name = #func_base, \
  913. .ops = &debug_##func_base##_ops }
  914. /**
  915. * debug_##func_base##_open() - Open debugfs entry for respective command
  916. * and event buffer.
  917. *
  918. * @inode: node for debug dir entry
  919. * @file: file handler
  920. *
  921. * Return: open status
  922. */
  923. #define GENERATE_DEBUG_STRUCTS(func_base) \
  924. static int debug_##func_base##_open(struct inode *inode, \
  925. struct file *file) \
  926. { \
  927. return single_open(file, debug_##func_base##_show, \
  928. inode->i_private); \
  929. } \
  930. \
  931. \
  932. static struct file_operations debug_##func_base##_ops = { \
  933. .open = debug_##func_base##_open, \
  934. .read = seq_read, \
  935. .llseek = seq_lseek, \
  936. .write = debug_##func_base##_write, \
  937. .release = single_release, \
  938. };
  939. GENERATE_DEBUG_STRUCTS(wmi_command_log);
  940. GENERATE_DEBUG_STRUCTS(wmi_command_tx_cmp_log);
  941. GENERATE_DEBUG_STRUCTS(wmi_event_log);
  942. GENERATE_DEBUG_STRUCTS(wmi_rx_event_log);
  943. GENERATE_DEBUG_STRUCTS(wmi_mgmt_command_log);
  944. GENERATE_DEBUG_STRUCTS(wmi_mgmt_command_tx_cmp_log);
  945. GENERATE_DEBUG_STRUCTS(wmi_mgmt_event_log);
  946. GENERATE_DEBUG_STRUCTS(wmi_enable);
  947. GENERATE_DEBUG_STRUCTS(wmi_log_size);
  948. struct wmi_debugfs_info wmi_debugfs_infos[] = {
  949. DEBUG_FOO(wmi_command_log),
  950. DEBUG_FOO(wmi_command_tx_cmp_log),
  951. DEBUG_FOO(wmi_event_log),
  952. DEBUG_FOO(wmi_rx_event_log),
  953. DEBUG_FOO(wmi_mgmt_command_log),
  954. DEBUG_FOO(wmi_mgmt_command_tx_cmp_log),
  955. DEBUG_FOO(wmi_mgmt_event_log),
  956. DEBUG_FOO(wmi_enable),
  957. DEBUG_FOO(wmi_log_size),
  958. };
  959. #define NUM_DEBUG_INFOS (sizeof(wmi_debugfs_infos) / \
  960. sizeof(wmi_debugfs_infos[0]))
  961. /**
  962. * wmi_debugfs_create() - Create debug_fs entry for wmi logging.
  963. *
  964. * @wmi_handle: wmi handle
  965. * @par_entry: debug directory entry
  966. * @id: Index to debug info data array
  967. *
  968. * Return: none
  969. */
  970. static void wmi_debugfs_create(wmi_unified_t wmi_handle,
  971. struct dentry *par_entry, int id)
  972. {
  973. int i;
  974. if (par_entry == NULL || (id < 0) || (id >= MAX_WMI_INSTANCES))
  975. goto out;
  976. for (i = 0; i < NUM_DEBUG_INFOS; ++i) {
  977. wmi_debugfs_infos[i].de[id] = debugfs_create_file(
  978. wmi_debugfs_infos[i].name, 0644, par_entry,
  979. wmi_handle, wmi_debugfs_infos[i].ops);
  980. if (wmi_debugfs_infos[i].de[id] == NULL) {
  981. qdf_print("%s: debug Entry creation failed!\n",
  982. __func__);
  983. goto out;
  984. }
  985. }
  986. return;
  987. out:
  988. qdf_print("%s: debug Entry creation failed!\n", __func__);
  989. wmi_log_buffer_free(wmi_handle);
  990. return;
  991. }
  992. /**
  993. * wmi_debugfs_remove() - Remove debugfs entry for wmi logging.
  994. * @wmi_handle: wmi handle
  995. * @dentry: debugfs directory entry
  996. * @id: Index to debug info data array
  997. *
  998. * Return: none
  999. */
  1000. static void wmi_debugfs_remove(wmi_unified_t wmi_handle)
  1001. {
  1002. int i;
  1003. struct dentry *dentry = wmi_handle->log_info.wmi_log_debugfs_dir;
  1004. int id;
  1005. if (!wmi_handle->log_info.wmi_instance_id)
  1006. return;
  1007. id = wmi_handle->log_info.wmi_instance_id - 1;
  1008. if (dentry && (!(id < 0) || (id >= MAX_WMI_INSTANCES))) {
  1009. for (i = 0; i < NUM_DEBUG_INFOS; ++i) {
  1010. if (wmi_debugfs_infos[i].de[id])
  1011. wmi_debugfs_infos[i].de[id] = NULL;
  1012. }
  1013. }
  1014. if (dentry)
  1015. debugfs_remove_recursive(dentry);
  1016. if (wmi_handle->log_info.wmi_instance_id)
  1017. wmi_handle->log_info.wmi_instance_id--;
  1018. }
  1019. /**
  1020. * wmi_debugfs_init() - debugfs functions to create debugfs directory and to
  1021. * create debugfs enteries.
  1022. *
  1023. * @h: wmi handler
  1024. *
  1025. * Return: init status
  1026. */
  1027. static QDF_STATUS wmi_debugfs_init(wmi_unified_t wmi_handle)
  1028. {
  1029. int wmi_index = wmi_handle->log_info.wmi_instance_id;
  1030. if (wmi_index < MAX_WMI_INSTANCES) {
  1031. wmi_handle->log_info.wmi_log_debugfs_dir =
  1032. debugfs_create_dir(debugfs_dir[wmi_index], NULL);
  1033. if (!wmi_handle->log_info.wmi_log_debugfs_dir) {
  1034. qdf_print("error while creating debugfs dir for %s\n",
  1035. debugfs_dir[wmi_index]);
  1036. return QDF_STATUS_E_FAILURE;
  1037. }
  1038. wmi_debugfs_create(wmi_handle,
  1039. wmi_handle->log_info.wmi_log_debugfs_dir,
  1040. wmi_index);
  1041. wmi_handle->log_info.wmi_instance_id++;
  1042. }
  1043. return QDF_STATUS_SUCCESS;
  1044. }
  1045. /**
  1046. * wmi_mgmt_cmd_record() - Wrapper function for mgmt command logging macro
  1047. *
  1048. * @wmi_handle: wmi handle
  1049. * @cmd: mgmt command
  1050. * @header: pointer to 802.11 header
  1051. * @vdev_id: vdev id
  1052. * @chanfreq: channel frequency
  1053. *
  1054. * Return: none
  1055. */
  1056. void wmi_mgmt_cmd_record(wmi_unified_t wmi_handle, uint32_t cmd,
  1057. void *header, uint32_t vdev_id, uint32_t chanfreq)
  1058. {
  1059. uint32_t data[CUSTOM_MGMT_CMD_DATA_SIZE];
  1060. data[0] = ((struct wmi_command_header *)header)->type;
  1061. data[1] = ((struct wmi_command_header *)header)->sub_type;
  1062. data[2] = vdev_id;
  1063. data[3] = chanfreq;
  1064. qdf_spin_lock_bh(&wmi_handle->log_info.wmi_record_lock);
  1065. WMI_MGMT_COMMAND_RECORD(wmi_handle, cmd, (uint8_t *)data);
  1066. qdf_spin_unlock_bh(&wmi_handle->log_info.wmi_record_lock);
  1067. }
  1068. #else
  1069. /**
  1070. * wmi_debugfs_remove() - Remove debugfs entry for wmi logging.
  1071. * @wmi_handle: wmi handle
  1072. * @dentry: debugfs directory entry
  1073. * @id: Index to debug info data array
  1074. *
  1075. * Return: none
  1076. */
  1077. static void wmi_debugfs_remove(wmi_unified_t wmi_handle) { }
  1078. void wmi_mgmt_cmd_record(wmi_unified_t wmi_handle, uint32_t cmd,
  1079. void *header, uint32_t vdev_id, uint32_t chanfreq) { }
  1080. static inline void wmi_log_buffer_free(struct wmi_unified *wmi_handle) { }
  1081. #endif /*WMI_INTERFACE_EVENT_LOGGING */
  1082. int wmi_get_host_credits(wmi_unified_t wmi_handle);
  1083. /* WMI buffer APIs */
  1084. #ifdef MEMORY_DEBUG
  1085. wmi_buf_t
  1086. wmi_buf_alloc_debug(wmi_unified_t wmi_handle, uint16_t len, uint8_t *file_name,
  1087. uint32_t line_num)
  1088. {
  1089. wmi_buf_t wmi_buf;
  1090. if (roundup(len + WMI_MIN_HEAD_ROOM, 4) > wmi_handle->max_msg_len) {
  1091. QDF_ASSERT(0);
  1092. return NULL;
  1093. }
  1094. wmi_buf = qdf_nbuf_alloc_debug(NULL,
  1095. roundup(len + WMI_MIN_HEAD_ROOM, 4),
  1096. WMI_MIN_HEAD_ROOM, 4, false, file_name,
  1097. line_num);
  1098. if (!wmi_buf)
  1099. return NULL;
  1100. /* Clear the wmi buffer */
  1101. OS_MEMZERO(qdf_nbuf_data(wmi_buf), len);
  1102. /*
  1103. * Set the length of the buffer to match the allocation size.
  1104. */
  1105. qdf_nbuf_set_pktlen(wmi_buf, len);
  1106. return wmi_buf;
  1107. }
  1108. void wmi_buf_free(wmi_buf_t net_buf)
  1109. {
  1110. qdf_nbuf_free(net_buf);
  1111. }
  1112. #else
  1113. wmi_buf_t wmi_buf_alloc(wmi_unified_t wmi_handle, uint16_t len)
  1114. {
  1115. wmi_buf_t wmi_buf;
  1116. if (roundup(len + WMI_MIN_HEAD_ROOM, 4) > wmi_handle->max_msg_len) {
  1117. QDF_ASSERT(0);
  1118. return NULL;
  1119. }
  1120. wmi_buf = qdf_nbuf_alloc(NULL, roundup(len + WMI_MIN_HEAD_ROOM, 4),
  1121. WMI_MIN_HEAD_ROOM, 4, false);
  1122. if (!wmi_buf)
  1123. return NULL;
  1124. /* Clear the wmi buffer */
  1125. OS_MEMZERO(qdf_nbuf_data(wmi_buf), len);
  1126. /*
  1127. * Set the length of the buffer to match the allocation size.
  1128. */
  1129. qdf_nbuf_set_pktlen(wmi_buf, len);
  1130. return wmi_buf;
  1131. }
  1132. void wmi_buf_free(wmi_buf_t net_buf)
  1133. {
  1134. qdf_nbuf_free(net_buf);
  1135. }
  1136. #endif
  1137. /**
  1138. * wmi_get_max_msg_len() - get maximum WMI message length
  1139. * @wmi_handle: WMI handle.
  1140. *
  1141. * This function returns the maximum WMI message length
  1142. *
  1143. * Return: maximum WMI message length
  1144. */
  1145. uint16_t wmi_get_max_msg_len(wmi_unified_t wmi_handle)
  1146. {
  1147. return wmi_handle->max_msg_len - WMI_MIN_HEAD_ROOM;
  1148. }
  1149. #ifndef WMI_CMD_STRINGS
  1150. static uint8_t *wmi_id_to_name(uint32_t wmi_command)
  1151. {
  1152. return "Invalid WMI cmd";
  1153. }
  1154. #endif
  1155. #ifdef CONFIG_MCL
  1156. static inline void wmi_log_cmd_id(uint32_t cmd_id, uint32_t tag)
  1157. {
  1158. WMI_LOGD("Send WMI command:%s command_id:%d htc_tag:%d\n",
  1159. wmi_id_to_name(cmd_id), cmd_id, tag);
  1160. }
  1161. /**
  1162. * wmi_is_pm_resume_cmd() - check if a cmd is part of the resume sequence
  1163. * @cmd_id: command to check
  1164. *
  1165. * Return: true if the command is part of the resume sequence.
  1166. */
  1167. static bool wmi_is_pm_resume_cmd(uint32_t cmd_id)
  1168. {
  1169. switch (cmd_id) {
  1170. case WMI_WOW_HOSTWAKEUP_FROM_SLEEP_CMDID:
  1171. case WMI_PDEV_RESUME_CMDID:
  1172. return true;
  1173. default:
  1174. return false;
  1175. }
  1176. }
  1177. #else
  1178. static bool wmi_is_pm_resume_cmd(uint32_t cmd_id)
  1179. {
  1180. return false;
  1181. }
  1182. #endif
  1183. /**
  1184. * wmi_unified_cmd_send() - WMI command API
  1185. * @wmi_handle: handle to wmi
  1186. * @buf: wmi buf
  1187. * @len: wmi buffer length
  1188. * @cmd_id: wmi command id
  1189. *
  1190. * Note, it is NOT safe to access buf after calling this function!
  1191. *
  1192. * Return: 0 on success
  1193. */
  1194. QDF_STATUS wmi_unified_cmd_send(wmi_unified_t wmi_handle, wmi_buf_t buf,
  1195. uint32_t len, uint32_t cmd_id)
  1196. {
  1197. HTC_PACKET *pkt;
  1198. QDF_STATUS status;
  1199. uint16_t htc_tag = 0;
  1200. if (wmi_get_runtime_pm_inprogress(wmi_handle)) {
  1201. htc_tag =
  1202. (A_UINT16)wmi_handle->ops->wmi_set_htc_tx_tag(
  1203. wmi_handle, buf, cmd_id);
  1204. } else if (qdf_atomic_read(&wmi_handle->is_target_suspended) &&
  1205. (!wmi_is_pm_resume_cmd(cmd_id))) {
  1206. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
  1207. "%s: Target is suspended", __func__);
  1208. QDF_ASSERT(0);
  1209. return QDF_STATUS_E_BUSY;
  1210. }
  1211. if (wmi_handle->wmi_stopinprogress) {
  1212. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
  1213. "WMI stop in progress\n");
  1214. return QDF_STATUS_E_INVAL;
  1215. }
  1216. #ifndef WMI_NON_TLV_SUPPORT
  1217. /* Do sanity check on the TLV parameter structure */
  1218. if (wmi_handle->target_type == WMI_TLV_TARGET) {
  1219. void *buf_ptr = (void *)qdf_nbuf_data(buf);
  1220. if (wmitlv_check_command_tlv_params(NULL, buf_ptr, len, cmd_id)
  1221. != 0) {
  1222. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
  1223. "\nERROR: %s: Invalid WMI Param Buffer for Cmd:%d",
  1224. __func__, cmd_id);
  1225. return QDF_STATUS_E_INVAL;
  1226. }
  1227. }
  1228. #endif
  1229. if (qdf_nbuf_push_head(buf, sizeof(WMI_CMD_HDR)) == NULL) {
  1230. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
  1231. "%s, Failed to send cmd %x, no memory",
  1232. __func__, cmd_id);
  1233. return QDF_STATUS_E_NOMEM;
  1234. }
  1235. WMI_SET_FIELD(qdf_nbuf_data(buf), WMI_CMD_HDR, COMMANDID, cmd_id);
  1236. qdf_atomic_inc(&wmi_handle->pending_cmds);
  1237. if (qdf_atomic_read(&wmi_handle->pending_cmds) >= WMI_MAX_CMDS) {
  1238. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
  1239. "\n%s: hostcredits = %d", __func__,
  1240. wmi_get_host_credits(wmi_handle));
  1241. htc_dump_counter_info(wmi_handle->htc_handle);
  1242. qdf_atomic_dec(&wmi_handle->pending_cmds);
  1243. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
  1244. "%s: MAX %d WMI Pending cmds reached.", __func__,
  1245. WMI_MAX_CMDS);
  1246. QDF_BUG(0);
  1247. return QDF_STATUS_E_BUSY;
  1248. }
  1249. pkt = qdf_mem_malloc(sizeof(*pkt));
  1250. if (!pkt) {
  1251. qdf_atomic_dec(&wmi_handle->pending_cmds);
  1252. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
  1253. "%s, Failed to alloc htc packet %x, no memory",
  1254. __func__, cmd_id);
  1255. return QDF_STATUS_E_NOMEM;
  1256. }
  1257. SET_HTC_PACKET_INFO_TX(pkt,
  1258. NULL,
  1259. qdf_nbuf_data(buf), len + sizeof(WMI_CMD_HDR),
  1260. wmi_handle->wmi_endpoint_id, htc_tag);
  1261. SET_HTC_PACKET_NET_BUF_CONTEXT(pkt, buf);
  1262. #ifdef CONFIG_MCL
  1263. wmi_log_cmd_id(cmd_id, htc_tag);
  1264. #endif
  1265. #ifdef WMI_INTERFACE_EVENT_LOGGING
  1266. if (wmi_handle->log_info.wmi_logging_enable) {
  1267. qdf_spin_lock_bh(&wmi_handle->log_info.wmi_record_lock);
  1268. /*Record 16 bytes of WMI cmd data -
  1269. * * exclude TLV and WMI headers */
  1270. if (wmi_handle->ops->is_management_record(cmd_id)) {
  1271. WMI_MGMT_COMMAND_RECORD(wmi_handle, cmd_id,
  1272. qdf_nbuf_data(buf) +
  1273. wmi_handle->log_info.buf_offset_command);
  1274. } else {
  1275. WMI_COMMAND_RECORD(wmi_handle, cmd_id,
  1276. qdf_nbuf_data(buf) +
  1277. wmi_handle->log_info.buf_offset_command);
  1278. }
  1279. qdf_spin_unlock_bh(&wmi_handle->log_info.wmi_record_lock);
  1280. }
  1281. #endif
  1282. status = htc_send_pkt(wmi_handle->htc_handle, pkt);
  1283. if (QDF_STATUS_SUCCESS != status) {
  1284. qdf_atomic_dec(&wmi_handle->pending_cmds);
  1285. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
  1286. "%s %d, htc_send_pkt failed", __func__, __LINE__);
  1287. qdf_mem_free(pkt);
  1288. return status;
  1289. }
  1290. return QDF_STATUS_SUCCESS;
  1291. }
  1292. /**
  1293. * wmi_unified_get_event_handler_ix() - gives event handler's index
  1294. * @wmi_handle: handle to wmi
  1295. * @event_id: wmi event id
  1296. *
  1297. * Return: event handler's index
  1298. */
  1299. static int wmi_unified_get_event_handler_ix(wmi_unified_t wmi_handle,
  1300. uint32_t event_id)
  1301. {
  1302. uint32_t idx = 0;
  1303. int32_t invalid_idx = -1;
  1304. struct wmi_soc *soc = wmi_handle->soc;
  1305. for (idx = 0; (idx < soc->max_event_idx &&
  1306. idx < WMI_UNIFIED_MAX_EVENT); ++idx) {
  1307. if (wmi_handle->event_id[idx] == event_id &&
  1308. wmi_handle->event_handler[idx] != NULL) {
  1309. return idx;
  1310. }
  1311. }
  1312. return invalid_idx;
  1313. }
  1314. /**
  1315. * wmi_unified_register_event() - register wmi event handler
  1316. * @wmi_handle: handle to wmi
  1317. * @event_id: wmi event id
  1318. * @handler_func: wmi event handler function
  1319. *
  1320. * Return: 0 on success
  1321. */
  1322. int wmi_unified_register_event(wmi_unified_t wmi_handle,
  1323. uint32_t event_id,
  1324. wmi_unified_event_handler handler_func)
  1325. {
  1326. uint32_t idx = 0;
  1327. uint32_t evt_id;
  1328. struct wmi_soc *soc = wmi_handle->soc;
  1329. if (event_id >= wmi_events_max ||
  1330. wmi_handle->wmi_events[event_id] == WMI_EVENT_ID_INVALID) {
  1331. qdf_print("%s: Event id %d is unavailable\n",
  1332. __func__, event_id);
  1333. return QDF_STATUS_E_FAILURE;
  1334. }
  1335. evt_id = wmi_handle->wmi_events[event_id];
  1336. if (wmi_unified_get_event_handler_ix(wmi_handle, evt_id) != -1) {
  1337. qdf_print("%s : event handler already registered 0x%x\n",
  1338. __func__, evt_id);
  1339. return QDF_STATUS_E_FAILURE;
  1340. }
  1341. if (soc->max_event_idx == WMI_UNIFIED_MAX_EVENT) {
  1342. qdf_print("%s : no more event handlers 0x%x\n",
  1343. __func__, evt_id);
  1344. return QDF_STATUS_E_FAILURE;
  1345. }
  1346. idx = soc->max_event_idx;
  1347. wmi_handle->event_handler[idx] = handler_func;
  1348. wmi_handle->event_id[idx] = evt_id;
  1349. qdf_spin_lock_bh(&soc->ctx_lock);
  1350. wmi_handle->ctx[idx] = WMI_RX_UMAC_CTX;
  1351. qdf_spin_unlock_bh(&soc->ctx_lock);
  1352. soc->max_event_idx++;
  1353. return 0;
  1354. }
  1355. /**
  1356. * wmi_unified_register_event_handler() - register wmi event handler
  1357. * @wmi_handle: handle to wmi
  1358. * @event_id: wmi event id
  1359. * @handler_func: wmi event handler function
  1360. * @rx_ctx: rx execution context for wmi rx events
  1361. *
  1362. * This API is to support legacy requirements. Will be deprecated in future.
  1363. * Return: 0 on success
  1364. */
  1365. int wmi_unified_register_event_handler(wmi_unified_t wmi_handle,
  1366. wmi_conv_event_id event_id,
  1367. wmi_unified_event_handler handler_func,
  1368. uint8_t rx_ctx)
  1369. {
  1370. uint32_t idx = 0;
  1371. uint32_t evt_id;
  1372. struct wmi_soc *soc = wmi_handle->soc;
  1373. if (event_id >= wmi_events_max ||
  1374. wmi_handle->wmi_events[event_id] == WMI_EVENT_ID_INVALID) {
  1375. qdf_print("%s: Event id %d is unavailable\n",
  1376. __func__, event_id);
  1377. return QDF_STATUS_E_FAILURE;
  1378. }
  1379. evt_id = wmi_handle->wmi_events[event_id];
  1380. if (wmi_unified_get_event_handler_ix(wmi_handle, evt_id) != -1) {
  1381. qdf_print("%s : event handler already registered 0x%x\n",
  1382. __func__, evt_id);
  1383. return QDF_STATUS_E_FAILURE;
  1384. }
  1385. if (soc->max_event_idx == WMI_UNIFIED_MAX_EVENT) {
  1386. qdf_print("%s : no more event handlers 0x%x\n",
  1387. __func__, evt_id);
  1388. return QDF_STATUS_E_FAILURE;
  1389. }
  1390. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_DEBUG,
  1391. "Registered event handler for event 0x%8x\n", evt_id);
  1392. idx = soc->max_event_idx;
  1393. wmi_handle->event_handler[idx] = handler_func;
  1394. wmi_handle->event_id[idx] = evt_id;
  1395. qdf_spin_lock_bh(&soc->ctx_lock);
  1396. wmi_handle->ctx[idx] = rx_ctx;
  1397. qdf_spin_unlock_bh(&soc->ctx_lock);
  1398. soc->max_event_idx++;
  1399. return 0;
  1400. }
  1401. /**
  1402. * wmi_unified_unregister_event() - unregister wmi event handler
  1403. * @wmi_handle: handle to wmi
  1404. * @event_id: wmi event id
  1405. *
  1406. * Return: 0 on success
  1407. */
  1408. int wmi_unified_unregister_event(wmi_unified_t wmi_handle,
  1409. uint32_t event_id)
  1410. {
  1411. uint32_t idx = 0;
  1412. uint32_t evt_id;
  1413. struct wmi_soc *soc = wmi_handle->soc;
  1414. if (event_id >= wmi_events_max ||
  1415. wmi_handle->wmi_events[event_id] == WMI_EVENT_ID_INVALID) {
  1416. qdf_print("%s: Event id %d is unavailable\n",
  1417. __func__, event_id);
  1418. return QDF_STATUS_E_FAILURE;
  1419. }
  1420. evt_id = wmi_handle->wmi_events[event_id];
  1421. idx = wmi_unified_get_event_handler_ix(wmi_handle, evt_id);
  1422. if (idx == -1) {
  1423. qdf_print("%s : event handler is not registered: evt id 0x%x\n",
  1424. __func__, evt_id);
  1425. return QDF_STATUS_E_FAILURE;
  1426. }
  1427. wmi_handle->event_handler[idx] = NULL;
  1428. wmi_handle->event_id[idx] = 0;
  1429. --soc->max_event_idx;
  1430. wmi_handle->event_handler[idx] =
  1431. wmi_handle->event_handler[soc->max_event_idx];
  1432. wmi_handle->event_id[idx] =
  1433. wmi_handle->event_id[soc->max_event_idx];
  1434. return 0;
  1435. }
  1436. /**
  1437. * wmi_unified_unregister_event_handler() - unregister wmi event handler
  1438. * @wmi_handle: handle to wmi
  1439. * @event_id: wmi event id
  1440. *
  1441. * Return: 0 on success
  1442. */
  1443. int wmi_unified_unregister_event_handler(wmi_unified_t wmi_handle,
  1444. wmi_conv_event_id event_id)
  1445. {
  1446. uint32_t idx = 0;
  1447. uint32_t evt_id;
  1448. struct wmi_soc *soc = wmi_handle->soc;
  1449. if (event_id >= wmi_events_max ||
  1450. wmi_handle->wmi_events[event_id] == WMI_EVENT_ID_INVALID) {
  1451. qdf_print("%s: Event id %d is unavailable\n",
  1452. __func__, event_id);
  1453. return QDF_STATUS_E_FAILURE;
  1454. }
  1455. evt_id = wmi_handle->wmi_events[event_id];
  1456. idx = wmi_unified_get_event_handler_ix(wmi_handle, evt_id);
  1457. if (idx == -1) {
  1458. qdf_print("%s : event handler is not registered: evt id 0x%x\n",
  1459. __func__, evt_id);
  1460. return QDF_STATUS_E_FAILURE;
  1461. }
  1462. wmi_handle->event_handler[idx] = NULL;
  1463. wmi_handle->event_id[idx] = 0;
  1464. --soc->max_event_idx;
  1465. wmi_handle->event_handler[idx] =
  1466. wmi_handle->event_handler[soc->max_event_idx];
  1467. wmi_handle->event_id[idx] =
  1468. wmi_handle->event_id[soc->max_event_idx];
  1469. return 0;
  1470. }
  1471. /**
  1472. * wmi_process_fw_event_default_ctx() - process in default caller context
  1473. * @wmi_handle: handle to wmi
  1474. * @htc_packet: pointer to htc packet
  1475. * @exec_ctx: execution context for wmi fw event
  1476. *
  1477. * Event process by below function will be in default caller context.
  1478. * wmi internally provides rx work thread processing context.
  1479. *
  1480. * Return: none
  1481. */
  1482. static void wmi_process_fw_event_default_ctx(struct wmi_unified *wmi_handle,
  1483. HTC_PACKET *htc_packet, uint8_t exec_ctx)
  1484. {
  1485. wmi_buf_t evt_buf;
  1486. evt_buf = (wmi_buf_t) htc_packet->pPktContext;
  1487. #ifndef CONFIG_MCL
  1488. wmi_handle->rx_ops.wma_process_fw_event_handler_cbk
  1489. (wmi_handle->scn_handle, evt_buf, exec_ctx);
  1490. #else
  1491. wmi_handle->rx_ops.wma_process_fw_event_handler_cbk(wmi_handle,
  1492. evt_buf, exec_ctx);
  1493. #endif
  1494. return;
  1495. }
  1496. /**
  1497. * wmi_process_fw_event_worker_thread_ctx() - process in worker thread context
  1498. * @wmi_handle: handle to wmi
  1499. * @htc_packet: pointer to htc packet
  1500. *
  1501. * Event process by below function will be in worker thread context.
  1502. * Use this method for events which are not critical and not
  1503. * handled in protocol stack.
  1504. *
  1505. * Return: none
  1506. */
  1507. static void wmi_process_fw_event_worker_thread_ctx
  1508. (struct wmi_unified *wmi_handle, HTC_PACKET *htc_packet)
  1509. {
  1510. wmi_buf_t evt_buf;
  1511. evt_buf = (wmi_buf_t) htc_packet->pPktContext;
  1512. qdf_spin_lock_bh(&wmi_handle->eventq_lock);
  1513. qdf_nbuf_queue_add(&wmi_handle->event_queue, evt_buf);
  1514. qdf_spin_unlock_bh(&wmi_handle->eventq_lock);
  1515. qdf_queue_work(0, wmi_handle->wmi_rx_work_queue,
  1516. &wmi_handle->rx_event_work);
  1517. return;
  1518. }
  1519. /**
  1520. * wmi_get_pdev_ep: Get wmi handle based on endpoint
  1521. * @soc: handle to wmi soc
  1522. * @ep: endpoint id
  1523. *
  1524. * Return: none
  1525. */
  1526. static struct wmi_unified *wmi_get_pdev_ep(struct wmi_soc *soc,
  1527. HTC_ENDPOINT_ID ep)
  1528. {
  1529. uint32_t i;
  1530. for (i = 0; i < WMI_MAX_RADIOS; i++)
  1531. if (soc->wmi_endpoint_id[i] == ep)
  1532. break;
  1533. if (i == WMI_MAX_RADIOS)
  1534. return NULL;
  1535. return soc->wmi_pdev[i];
  1536. }
  1537. /**
  1538. * wmi_control_rx() - process fw events callbacks
  1539. * @ctx: handle to wmi
  1540. * @htc_packet: pointer to htc packet
  1541. *
  1542. * Return: none
  1543. */
  1544. static void wmi_control_rx(void *ctx, HTC_PACKET *htc_packet)
  1545. {
  1546. struct wmi_soc *soc = (struct wmi_soc *) ctx;
  1547. struct wmi_unified *wmi_handle;
  1548. wmi_buf_t evt_buf;
  1549. uint32_t id;
  1550. uint32_t idx = 0;
  1551. enum wmi_rx_exec_ctx exec_ctx;
  1552. evt_buf = (wmi_buf_t) htc_packet->pPktContext;
  1553. wmi_handle = wmi_get_pdev_ep(soc, htc_packet->Endpoint);
  1554. if (wmi_handle == NULL) {
  1555. qdf_print
  1556. ("%s :unable to get wmi_handle to Endpoint %d\n",
  1557. __func__, htc_packet->Endpoint);
  1558. qdf_nbuf_free(evt_buf);
  1559. return;
  1560. }
  1561. id = WMI_GET_FIELD(qdf_nbuf_data(evt_buf), WMI_CMD_HDR, COMMANDID);
  1562. idx = wmi_unified_get_event_handler_ix(wmi_handle, id);
  1563. if (qdf_unlikely(idx == A_ERROR)) {
  1564. WMI_LOGD("%s :event handler is not registered: event id 0x%x\n",
  1565. __func__, id);
  1566. qdf_nbuf_free(evt_buf);
  1567. return;
  1568. }
  1569. qdf_spin_lock_bh(&soc->ctx_lock);
  1570. exec_ctx = wmi_handle->ctx[idx];
  1571. qdf_spin_unlock_bh(&soc->ctx_lock);
  1572. #ifdef WMI_INTERFACE_EVENT_LOGGING
  1573. if (wmi_handle->log_info.wmi_logging_enable) {
  1574. uint8_t *data;
  1575. data = qdf_nbuf_data(evt_buf);
  1576. qdf_spin_lock_bh(&wmi_handle->log_info.wmi_record_lock);
  1577. /* Exclude 4 bytes of TLV header */
  1578. WMI_RX_EVENT_RECORD(wmi_handle, id, data +
  1579. wmi_handle->log_info.buf_offset_event);
  1580. qdf_spin_unlock_bh(&wmi_handle->log_info.wmi_record_lock);
  1581. }
  1582. #endif
  1583. if (exec_ctx == WMI_RX_WORK_CTX) {
  1584. wmi_process_fw_event_worker_thread_ctx
  1585. (wmi_handle, htc_packet);
  1586. } else if (exec_ctx > WMI_RX_WORK_CTX) {
  1587. wmi_process_fw_event_default_ctx
  1588. (wmi_handle, htc_packet, exec_ctx);
  1589. } else {
  1590. qdf_print("%s :Invalid event context %d\n", __func__, exec_ctx);
  1591. qdf_nbuf_free(evt_buf);
  1592. }
  1593. }
  1594. /**
  1595. * wmi_process_fw_event() - process any fw event
  1596. * @wmi_handle: wmi handle
  1597. * @evt_buf: fw event buffer
  1598. *
  1599. * This function process fw event in caller context
  1600. *
  1601. * Return: none
  1602. */
  1603. void wmi_process_fw_event(struct wmi_unified *wmi_handle, wmi_buf_t evt_buf)
  1604. {
  1605. __wmi_control_rx(wmi_handle, evt_buf);
  1606. }
  1607. /**
  1608. * __wmi_control_rx() - process serialize wmi event callback
  1609. * @wmi_handle: wmi handle
  1610. * @evt_buf: fw event buffer
  1611. *
  1612. * Return: none
  1613. */
  1614. void __wmi_control_rx(struct wmi_unified *wmi_handle, wmi_buf_t evt_buf)
  1615. {
  1616. uint32_t id;
  1617. uint8_t *data;
  1618. uint32_t len;
  1619. void *wmi_cmd_struct_ptr = NULL;
  1620. #ifndef WMI_NON_TLV_SUPPORT
  1621. int tlv_ok_status = 0;
  1622. #endif
  1623. uint32_t idx = 0;
  1624. id = WMI_GET_FIELD(qdf_nbuf_data(evt_buf), WMI_CMD_HDR, COMMANDID);
  1625. if (qdf_nbuf_pull_head(evt_buf, sizeof(WMI_CMD_HDR)) == NULL)
  1626. goto end;
  1627. data = qdf_nbuf_data(evt_buf);
  1628. len = qdf_nbuf_len(evt_buf);
  1629. #ifndef WMI_NON_TLV_SUPPORT
  1630. if (wmi_handle->target_type == WMI_TLV_TARGET) {
  1631. /* Validate and pad(if necessary) the TLVs */
  1632. tlv_ok_status =
  1633. wmitlv_check_and_pad_event_tlvs(wmi_handle->scn_handle,
  1634. data, len, id,
  1635. &wmi_cmd_struct_ptr);
  1636. if (tlv_ok_status != 0) {
  1637. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
  1638. "%s: Error: id=0x%d, wmitlv check status=%d\n",
  1639. __func__, id, tlv_ok_status);
  1640. goto end;
  1641. }
  1642. }
  1643. #endif
  1644. idx = wmi_unified_get_event_handler_ix(wmi_handle, id);
  1645. if (idx == A_ERROR) {
  1646. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_ERROR,
  1647. "%s : event handler is not registered: event id 0x%x\n",
  1648. __func__, id);
  1649. goto end;
  1650. }
  1651. #ifdef WMI_INTERFACE_EVENT_LOGGING
  1652. if (wmi_handle->log_info.wmi_logging_enable) {
  1653. qdf_spin_lock_bh(&wmi_handle->log_info.wmi_record_lock);
  1654. /* Exclude 4 bytes of TLV header */
  1655. if (wmi_handle->ops->is_management_record(id)) {
  1656. WMI_MGMT_EVENT_RECORD(wmi_handle, id, data
  1657. + wmi_handle->log_info.buf_offset_event);
  1658. } else {
  1659. WMI_EVENT_RECORD(wmi_handle, id, data +
  1660. wmi_handle->log_info.buf_offset_event);
  1661. }
  1662. qdf_spin_unlock_bh(&wmi_handle->log_info.wmi_record_lock);
  1663. }
  1664. #endif
  1665. /* Call the WMI registered event handler */
  1666. if (wmi_handle->target_type == WMI_TLV_TARGET)
  1667. wmi_handle->event_handler[idx] (wmi_handle->scn_handle,
  1668. wmi_cmd_struct_ptr, len);
  1669. else
  1670. wmi_handle->event_handler[idx] (wmi_handle->scn_handle,
  1671. data, len);
  1672. end:
  1673. /* Free event buffer and allocated event tlv */
  1674. #ifndef WMI_NON_TLV_SUPPORT
  1675. if (wmi_handle->target_type == WMI_TLV_TARGET)
  1676. wmitlv_free_allocated_event_tlvs(id, &wmi_cmd_struct_ptr);
  1677. #endif
  1678. qdf_nbuf_free(evt_buf);
  1679. }
  1680. #define WMI_WQ_WD_TIMEOUT (10 * 1000) /* 10s */
  1681. static inline void wmi_workqueue_watchdog_warn(uint32_t msg_type_id)
  1682. {
  1683. QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
  1684. "%s: Message type %x has exceeded its alloted time of %ds",
  1685. __func__, msg_type_id, WMI_WQ_WD_TIMEOUT / 1000);
  1686. }
  1687. #ifdef CONFIG_SLUB_DEBUG_ON
  1688. static void wmi_workqueue_watchdog_bite(void *arg)
  1689. {
  1690. struct wmi_wq_dbg_info *info = arg;
  1691. wmi_workqueue_watchdog_warn(info->wd_msg_type_id);
  1692. qdf_print_thread_trace(info->task);
  1693. QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
  1694. "%s: Going down for WMI WQ Watchdog Bite!", __func__);
  1695. QDF_BUG(0);
  1696. }
  1697. #else
  1698. static inline void wmi_workqueue_watchdog_bite(void *arg)
  1699. {
  1700. struct wmi_wq_dbg_info *info = arg;
  1701. wmi_workqueue_watchdog_warn(info->wd_msg_type_id);
  1702. }
  1703. #endif
  1704. /**
  1705. * wmi_rx_event_work() - process rx event in rx work queue context
  1706. * @arg: opaque pointer to wmi handle
  1707. *
  1708. * This function process any fw event to serialize it through rx worker thread.
  1709. *
  1710. * Return: none
  1711. */
  1712. static void wmi_rx_event_work(void *arg)
  1713. {
  1714. wmi_buf_t buf;
  1715. struct wmi_unified *wmi = arg;
  1716. qdf_timer_t wd_timer;
  1717. struct wmi_wq_dbg_info info;
  1718. /* initialize WMI workqueue watchdog timer */
  1719. qdf_timer_init(NULL, &wd_timer, &wmi_workqueue_watchdog_bite,
  1720. &info, QDF_TIMER_TYPE_SW);
  1721. qdf_spin_lock_bh(&wmi->eventq_lock);
  1722. buf = qdf_nbuf_queue_remove(&wmi->event_queue);
  1723. qdf_spin_unlock_bh(&wmi->eventq_lock);
  1724. while (buf) {
  1725. qdf_timer_start(&wd_timer, WMI_WQ_WD_TIMEOUT);
  1726. info.wd_msg_type_id =
  1727. WMI_GET_FIELD(qdf_nbuf_data(buf), WMI_CMD_HDR, COMMANDID);
  1728. info.wmi_wq = wmi->wmi_rx_work_queue;
  1729. info.task = qdf_get_current_task();
  1730. __wmi_control_rx(wmi, buf);
  1731. qdf_timer_stop(&wd_timer);
  1732. qdf_spin_lock_bh(&wmi->eventq_lock);
  1733. buf = qdf_nbuf_queue_remove(&wmi->event_queue);
  1734. qdf_spin_unlock_bh(&wmi->eventq_lock);
  1735. }
  1736. qdf_timer_free(&wd_timer);
  1737. }
  1738. #ifdef FEATURE_RUNTIME_PM
  1739. /**
  1740. * wmi_runtime_pm_init() - initialize runtime pm wmi variables
  1741. * @wmi_handle: wmi context
  1742. */
  1743. static void wmi_runtime_pm_init(struct wmi_unified *wmi_handle)
  1744. {
  1745. qdf_atomic_init(&wmi_handle->runtime_pm_inprogress);
  1746. }
  1747. /**
  1748. * wmi_set_runtime_pm_inprogress() - set runtime pm progress flag
  1749. * @wmi_handle: wmi context
  1750. * @val: runtime pm progress flag
  1751. */
  1752. void wmi_set_runtime_pm_inprogress(wmi_unified_t wmi_handle, A_BOOL val)
  1753. {
  1754. qdf_atomic_set(&wmi_handle->runtime_pm_inprogress, val);
  1755. }
  1756. /**
  1757. * wmi_get_runtime_pm_inprogress() - get runtime pm progress flag
  1758. * @wmi_handle: wmi context
  1759. */
  1760. inline bool wmi_get_runtime_pm_inprogress(wmi_unified_t wmi_handle)
  1761. {
  1762. return qdf_atomic_read(&wmi_handle->runtime_pm_inprogress);
  1763. }
  1764. #else
  1765. static void wmi_runtime_pm_init(struct wmi_unified *wmi_handle)
  1766. {
  1767. }
  1768. #endif
  1769. /**
  1770. * wmi_unified_get_soc_handle: Get WMI SoC handle
  1771. * @param wmi_handle: WMI context got from wmi_attach
  1772. *
  1773. * return: Pointer to Soc handle
  1774. */
  1775. void *wmi_unified_get_soc_handle(struct wmi_unified *wmi_handle)
  1776. {
  1777. return wmi_handle->soc;
  1778. }
  1779. /**
  1780. * wmi_interface_logging_init: Interface looging init
  1781. * @param wmi_handle: Pointer to wmi handle object
  1782. *
  1783. * return: None
  1784. */
  1785. #ifdef WMI_INTERFACE_EVENT_LOGGING
  1786. static inline void wmi_interface_logging_init(struct wmi_unified *wmi_handle)
  1787. {
  1788. if (QDF_STATUS_SUCCESS == wmi_log_init(wmi_handle)) {
  1789. qdf_spinlock_create(&wmi_handle->log_info.wmi_record_lock);
  1790. wmi_debugfs_init(wmi_handle);
  1791. }
  1792. }
  1793. #else
  1794. void wmi_interface_logging_init(struct wmi_unified *wmi_handle)
  1795. {
  1796. }
  1797. #endif
  1798. /**
  1799. * wmi_target_params_init: Target specific params init
  1800. * @param wmi_soc: Pointer to wmi soc object
  1801. * @param wmi_handle: Pointer to wmi handle object
  1802. *
  1803. * return: None
  1804. */
  1805. #ifndef CONFIG_MCL
  1806. static inline void wmi_target_params_init(struct wmi_soc *soc,
  1807. struct wmi_unified *wmi_handle)
  1808. {
  1809. wmi_handle->pdev_param = soc->pdev_param;
  1810. wmi_handle->vdev_param = soc->vdev_param;
  1811. wmi_handle->services = soc->services;
  1812. }
  1813. #else
  1814. static inline void wmi_target_params_init(struct wmi_soc *soc,
  1815. struct wmi_unified *wmi_handle)
  1816. {
  1817. }
  1818. #endif
  1819. /**
  1820. * wmi_unified_get_pdev_handle: Get WMI SoC handle
  1821. * @param wmi_soc: Pointer to wmi soc object
  1822. * @param pdev_idx: pdev index
  1823. *
  1824. * return: Pointer to wmi handle or NULL on failure
  1825. */
  1826. void *wmi_unified_get_pdev_handle(struct wmi_soc *soc, uint32_t pdev_idx)
  1827. {
  1828. struct wmi_unified *wmi_handle;
  1829. if (pdev_idx >= WMI_MAX_RADIOS)
  1830. return NULL;
  1831. if (soc->wmi_pdev[pdev_idx] == NULL) {
  1832. wmi_handle =
  1833. (struct wmi_unified *) qdf_mem_malloc(
  1834. sizeof(struct wmi_unified));
  1835. if (wmi_handle == NULL) {
  1836. qdf_print("allocation of wmi handle failed %zu\n",
  1837. sizeof(struct wmi_unified));
  1838. return NULL;
  1839. }
  1840. wmi_handle->scn_handle = soc->scn_handle;
  1841. wmi_handle->event_id = soc->event_id;
  1842. wmi_handle->event_handler = soc->event_handler;
  1843. wmi_handle->ctx = soc->ctx;
  1844. wmi_handle->ops = soc->ops;
  1845. qdf_spinlock_create(&wmi_handle->eventq_lock);
  1846. qdf_nbuf_queue_init(&wmi_handle->event_queue);
  1847. qdf_create_work(0, &wmi_handle->rx_event_work,
  1848. wmi_rx_event_work, wmi_handle);
  1849. wmi_handle->wmi_rx_work_queue =
  1850. qdf_create_workqueue("wmi_rx_event_work_queue");
  1851. if (NULL == wmi_handle->wmi_rx_work_queue) {
  1852. WMI_LOGE("failed to create wmi_rx_event_work_queue");
  1853. goto error;
  1854. }
  1855. wmi_handle->wmi_events = soc->wmi_events;
  1856. wmi_target_params_init(soc, wmi_handle);
  1857. wmi_interface_logging_init(wmi_handle);
  1858. qdf_atomic_init(&wmi_handle->pending_cmds);
  1859. qdf_atomic_init(&wmi_handle->is_target_suspended);
  1860. wmi_handle->target_type = soc->target_type;
  1861. wmi_handle->soc = soc;
  1862. soc->wmi_pdev[pdev_idx] = wmi_handle;
  1863. } else
  1864. wmi_handle = soc->wmi_pdev[pdev_idx];
  1865. wmi_handle->wmi_stopinprogress = 0;
  1866. wmi_handle->wmi_endpoint_id = soc->wmi_endpoint_id[pdev_idx];
  1867. wmi_handle->htc_handle = soc->htc_handle;
  1868. wmi_handle->max_msg_len = soc->max_msg_len[pdev_idx];
  1869. return wmi_handle;
  1870. error:
  1871. qdf_mem_free(wmi_handle);
  1872. return NULL;
  1873. }
  1874. /**
  1875. * wmi_unified_attach() - attach for unified WMI
  1876. * @scn_handle: handle to SCN
  1877. * @osdev: OS device context
  1878. * @target_type: TLV or not-TLV based target
  1879. * @use_cookie: cookie based allocation enabled/disabled
  1880. * @ops: umac rx callbacks
  1881. * @psoc: objmgr psoc
  1882. *
  1883. * @Return: wmi handle.
  1884. */
  1885. void *wmi_unified_attach(void *scn_handle,
  1886. osdev_t osdev, enum wmi_target_type target_type,
  1887. bool use_cookie, struct wmi_rx_ops *rx_ops,
  1888. struct wlan_objmgr_psoc *psoc)
  1889. {
  1890. struct wmi_unified *wmi_handle;
  1891. struct wmi_soc *soc;
  1892. soc = (struct wmi_soc *) qdf_mem_malloc(sizeof(struct wmi_soc));
  1893. if (soc == NULL) {
  1894. qdf_print("Allocation of wmi_soc failed %zu\n",
  1895. sizeof(struct wmi_soc));
  1896. return NULL;
  1897. }
  1898. wmi_handle =
  1899. (struct wmi_unified *) qdf_mem_malloc(
  1900. sizeof(struct wmi_unified));
  1901. if (wmi_handle == NULL) {
  1902. qdf_mem_free(soc);
  1903. qdf_print("allocation of wmi handle failed %zu\n",
  1904. sizeof(struct wmi_unified));
  1905. return NULL;
  1906. }
  1907. wmi_handle->soc = soc;
  1908. wmi_handle->event_id = soc->event_id;
  1909. wmi_handle->event_handler = soc->event_handler;
  1910. wmi_handle->ctx = soc->ctx;
  1911. wmi_handle->wmi_events = soc->wmi_events;
  1912. wmi_target_params_init(soc, wmi_handle);
  1913. wmi_handle->scn_handle = scn_handle;
  1914. soc->scn_handle = scn_handle;
  1915. qdf_atomic_init(&wmi_handle->pending_cmds);
  1916. qdf_atomic_init(&wmi_handle->is_target_suspended);
  1917. wmi_runtime_pm_init(wmi_handle);
  1918. qdf_spinlock_create(&wmi_handle->eventq_lock);
  1919. qdf_nbuf_queue_init(&wmi_handle->event_queue);
  1920. qdf_create_work(0, &wmi_handle->rx_event_work,
  1921. wmi_rx_event_work, wmi_handle);
  1922. wmi_handle->wmi_rx_work_queue =
  1923. qdf_create_workqueue("wmi_rx_event_work_queue");
  1924. if (NULL == wmi_handle->wmi_rx_work_queue) {
  1925. WMI_LOGE("failed to create wmi_rx_event_work_queue");
  1926. goto error;
  1927. }
  1928. wmi_interface_logging_init(wmi_handle);
  1929. /* Attach mc_thread context processing function */
  1930. wmi_handle->rx_ops.wma_process_fw_event_handler_cbk =
  1931. rx_ops->wma_process_fw_event_handler_cbk;
  1932. wmi_handle->target_type = target_type;
  1933. soc->target_type = target_type;
  1934. if (target_type == WMI_TLV_TARGET)
  1935. wmi_tlv_attach(wmi_handle);
  1936. else
  1937. wmi_non_tlv_attach(wmi_handle);
  1938. /* Assign target cookie capablity */
  1939. wmi_handle->use_cookie = use_cookie;
  1940. wmi_handle->osdev = osdev;
  1941. wmi_handle->wmi_stopinprogress = 0;
  1942. /* Increase the ref count once refcount infra is present */
  1943. soc->wmi_psoc = psoc;
  1944. qdf_spinlock_create(&soc->ctx_lock);
  1945. soc->ops = wmi_handle->ops;
  1946. soc->wmi_pdev[0] = wmi_handle;
  1947. return wmi_handle;
  1948. error:
  1949. qdf_mem_free(soc);
  1950. qdf_mem_free(wmi_handle);
  1951. return NULL;
  1952. }
  1953. /**
  1954. * wmi_unified_detach() - detach for unified WMI
  1955. *
  1956. * @wmi_handle : handle to wmi.
  1957. *
  1958. * @Return: none.
  1959. */
  1960. void wmi_unified_detach(struct wmi_unified *wmi_handle)
  1961. {
  1962. wmi_buf_t buf;
  1963. struct wmi_soc *soc;
  1964. uint8_t i;
  1965. soc = wmi_handle->soc;
  1966. for (i = 0; i < WMI_MAX_RADIOS; i++) {
  1967. if (soc->wmi_pdev[i]) {
  1968. qdf_flush_workqueue(0,
  1969. soc->wmi_pdev[i]->wmi_rx_work_queue);
  1970. qdf_destroy_workqueue(0,
  1971. soc->wmi_pdev[i]->wmi_rx_work_queue);
  1972. wmi_debugfs_remove(soc->wmi_pdev[i]);
  1973. buf = qdf_nbuf_queue_remove(
  1974. &soc->wmi_pdev[i]->event_queue);
  1975. while (buf) {
  1976. qdf_nbuf_free(buf);
  1977. buf = qdf_nbuf_queue_remove(
  1978. &soc->wmi_pdev[i]->event_queue);
  1979. }
  1980. wmi_log_buffer_free(soc->wmi_pdev[i]);
  1981. /* Free events logs list */
  1982. if (soc->wmi_pdev[i]->events_logs_list)
  1983. qdf_mem_free(
  1984. soc->wmi_pdev[i]->events_logs_list);
  1985. qdf_spinlock_destroy(&soc->wmi_pdev[i]->eventq_lock);
  1986. qdf_mem_free(soc->wmi_pdev[i]);
  1987. }
  1988. }
  1989. qdf_spinlock_destroy(&soc->ctx_lock);
  1990. if (soc->wmi_service_bitmap) {
  1991. qdf_mem_free(soc->wmi_service_bitmap);
  1992. soc->wmi_service_bitmap = NULL;
  1993. }
  1994. if (soc->wmi_ext_service_bitmap) {
  1995. qdf_mem_free(soc->wmi_ext_service_bitmap);
  1996. soc->wmi_ext_service_bitmap = NULL;
  1997. }
  1998. /* Decrease the ref count once refcount infra is present */
  1999. soc->wmi_psoc = NULL;
  2000. qdf_mem_free(soc);
  2001. }
  2002. /**
  2003. * wmi_unified_remove_work() - detach for WMI work
  2004. * @wmi_handle: handle to WMI
  2005. *
  2006. * A function that does not fully detach WMI, but just remove work
  2007. * queue items associated with it. This is used to make sure that
  2008. * before any other processing code that may destroy related contexts
  2009. * (HTC, etc), work queue processing on WMI has already been stopped.
  2010. *
  2011. * Return: None
  2012. */
  2013. void
  2014. wmi_unified_remove_work(struct wmi_unified *wmi_handle)
  2015. {
  2016. wmi_buf_t buf;
  2017. qdf_flush_workqueue(0, wmi_handle->wmi_rx_work_queue);
  2018. qdf_spin_lock_bh(&wmi_handle->eventq_lock);
  2019. buf = qdf_nbuf_queue_remove(&wmi_handle->event_queue);
  2020. while (buf) {
  2021. qdf_nbuf_free(buf);
  2022. buf = qdf_nbuf_queue_remove(&wmi_handle->event_queue);
  2023. }
  2024. qdf_spin_unlock_bh(&wmi_handle->eventq_lock);
  2025. }
  2026. /**
  2027. * wmi_htc_tx_complete() - Process htc tx completion
  2028. *
  2029. * @ctx: handle to wmi
  2030. * @htc_packet: pointer to htc packet
  2031. *
  2032. * @Return: none.
  2033. */
  2034. static void wmi_htc_tx_complete(void *ctx, HTC_PACKET *htc_pkt)
  2035. {
  2036. struct wmi_soc *soc = (struct wmi_soc *) ctx;
  2037. wmi_buf_t wmi_cmd_buf = GET_HTC_PACKET_NET_BUF_CONTEXT(htc_pkt);
  2038. u_int8_t *buf_ptr;
  2039. u_int32_t len;
  2040. struct wmi_unified *wmi_handle;
  2041. #ifdef WMI_INTERFACE_EVENT_LOGGING
  2042. uint32_t cmd_id;
  2043. #endif
  2044. ASSERT(wmi_cmd_buf);
  2045. wmi_handle = wmi_get_pdev_ep(soc, htc_pkt->Endpoint);
  2046. if (wmi_handle == NULL) {
  2047. WMI_LOGE("%s: Unable to get wmi handle\n", __func__);
  2048. QDF_ASSERT(0);
  2049. return;
  2050. }
  2051. #ifdef WMI_INTERFACE_EVENT_LOGGING
  2052. if (wmi_handle && wmi_handle->log_info.wmi_logging_enable) {
  2053. cmd_id = WMI_GET_FIELD(qdf_nbuf_data(wmi_cmd_buf),
  2054. WMI_CMD_HDR, COMMANDID);
  2055. qdf_spin_lock_bh(&wmi_handle->log_info.wmi_record_lock);
  2056. /* Record 16 bytes of WMI cmd tx complete data
  2057. - exclude TLV and WMI headers */
  2058. if (wmi_handle->ops->is_management_record(cmd_id)) {
  2059. WMI_MGMT_COMMAND_TX_CMP_RECORD(wmi_handle, cmd_id,
  2060. qdf_nbuf_data(wmi_cmd_buf) +
  2061. wmi_handle->log_info.buf_offset_command);
  2062. } else {
  2063. WMI_COMMAND_TX_CMP_RECORD(wmi_handle, cmd_id,
  2064. qdf_nbuf_data(wmi_cmd_buf) +
  2065. wmi_handle->log_info.buf_offset_command);
  2066. }
  2067. qdf_spin_unlock_bh(&wmi_handle->log_info.wmi_record_lock);
  2068. }
  2069. #endif
  2070. buf_ptr = (u_int8_t *) wmi_buf_data(wmi_cmd_buf);
  2071. len = qdf_nbuf_len(wmi_cmd_buf);
  2072. qdf_mem_zero(buf_ptr, len);
  2073. qdf_nbuf_free(wmi_cmd_buf);
  2074. qdf_mem_free(htc_pkt);
  2075. qdf_atomic_dec(&wmi_handle->pending_cmds);
  2076. }
  2077. /**
  2078. * wmi_connect_pdev_htc_service() - WMI API to get connect to HTC service
  2079. *
  2080. * @wmi_handle: handle to WMI.
  2081. * @pdev_idx: Pdev index
  2082. *
  2083. * @Return: status.
  2084. */
  2085. static int wmi_connect_pdev_htc_service(struct wmi_soc *soc,
  2086. uint32_t pdev_idx)
  2087. {
  2088. int status;
  2089. uint32_t svc_id[] = {WMI_CONTROL_SVC, WMI_CONTROL_SVC_WMAC1,
  2090. WMI_CONTROL_SVC_WMAC2};
  2091. struct htc_service_connect_resp response;
  2092. struct htc_service_connect_req connect;
  2093. OS_MEMZERO(&connect, sizeof(connect));
  2094. OS_MEMZERO(&response, sizeof(response));
  2095. /* meta data is unused for now */
  2096. connect.pMetaData = NULL;
  2097. connect.MetaDataLength = 0;
  2098. /* these fields are the same for all service endpoints */
  2099. connect.EpCallbacks.pContext = soc;
  2100. connect.EpCallbacks.EpTxCompleteMultiple =
  2101. NULL /* Control path completion ar6000_tx_complete */;
  2102. connect.EpCallbacks.EpRecv = wmi_control_rx /* Control path rx */;
  2103. connect.EpCallbacks.EpRecvRefill = NULL /* ar6000_rx_refill */;
  2104. connect.EpCallbacks.EpSendFull = NULL /* ar6000_tx_queue_full */;
  2105. connect.EpCallbacks.EpTxComplete =
  2106. wmi_htc_tx_complete /* ar6000_tx_queue_full */;
  2107. /* connect to control service */
  2108. connect.service_id = svc_id[pdev_idx];
  2109. status = htc_connect_service(soc->htc_handle, &connect,
  2110. &response);
  2111. if (status != EOK) {
  2112. qdf_print
  2113. ("Failed to connect to WMI CONTROL service status:%d\n",
  2114. status);
  2115. return status;
  2116. }
  2117. soc->wmi_endpoint_id[pdev_idx] = response.Endpoint;
  2118. soc->max_msg_len[pdev_idx] = response.MaxMsgLength;
  2119. return 0;
  2120. }
  2121. /**
  2122. * wmi_unified_connect_htc_service() - WMI API to get connect to HTC service
  2123. *
  2124. * @wmi_handle: handle to WMI.
  2125. *
  2126. * @Return: status.
  2127. */
  2128. QDF_STATUS
  2129. wmi_unified_connect_htc_service(struct wmi_unified *wmi_handle,
  2130. void *htc_handle)
  2131. {
  2132. uint32_t i;
  2133. uint8_t wmi_ep_count;
  2134. wmi_handle->soc->htc_handle = htc_handle;
  2135. wmi_ep_count = htc_get_wmi_endpoint_count(htc_handle);
  2136. if (wmi_ep_count > WMI_MAX_RADIOS)
  2137. return QDF_STATUS_E_FAULT;
  2138. for (i = 0; i < wmi_ep_count; i++)
  2139. wmi_connect_pdev_htc_service(wmi_handle->soc, i);
  2140. wmi_handle->htc_handle = htc_handle;
  2141. wmi_handle->wmi_endpoint_id = wmi_handle->soc->wmi_endpoint_id[0];
  2142. wmi_handle->max_msg_len = wmi_handle->soc->max_msg_len[0];
  2143. return QDF_STATUS_SUCCESS;
  2144. }
  2145. /**
  2146. * wmi_get_host_credits() - WMI API to get updated host_credits
  2147. *
  2148. * @wmi_handle: handle to WMI.
  2149. *
  2150. * @Return: updated host_credits.
  2151. */
  2152. int wmi_get_host_credits(wmi_unified_t wmi_handle)
  2153. {
  2154. int host_credits = 0;
  2155. htc_get_control_endpoint_tx_host_credits(wmi_handle->htc_handle,
  2156. &host_credits);
  2157. return host_credits;
  2158. }
  2159. /**
  2160. * wmi_get_pending_cmds() - WMI API to get WMI Pending Commands in the HTC
  2161. * queue
  2162. *
  2163. * @wmi_handle: handle to WMI.
  2164. *
  2165. * @Return: Pending Commands in the HTC queue.
  2166. */
  2167. int wmi_get_pending_cmds(wmi_unified_t wmi_handle)
  2168. {
  2169. return qdf_atomic_read(&wmi_handle->pending_cmds);
  2170. }
  2171. /**
  2172. * wmi_set_target_suspend() - WMI API to set target suspend state
  2173. *
  2174. * @wmi_handle: handle to WMI.
  2175. * @val: suspend state boolean.
  2176. *
  2177. * @Return: none.
  2178. */
  2179. void wmi_set_target_suspend(wmi_unified_t wmi_handle, A_BOOL val)
  2180. {
  2181. qdf_atomic_set(&wmi_handle->is_target_suspended, val);
  2182. }
  2183. /**
  2184. * WMI API to set crash injection state
  2185. * @param wmi_handle: handle to WMI.
  2186. * @param val: crash injection state boolean.
  2187. */
  2188. void wmi_tag_crash_inject(wmi_unified_t wmi_handle, A_BOOL flag)
  2189. {
  2190. wmi_handle->tag_crash_inject = flag;
  2191. }
  2192. /**
  2193. * WMI API to set bus suspend state
  2194. * @param wmi_handle: handle to WMI.
  2195. * @param val: suspend state boolean.
  2196. */
  2197. void wmi_set_is_wow_bus_suspended(wmi_unified_t wmi_handle, A_BOOL val)
  2198. {
  2199. qdf_atomic_set(&wmi_handle->is_wow_bus_suspended, val);
  2200. }
  2201. void wmi_set_tgt_assert(wmi_unified_t wmi_handle, bool val)
  2202. {
  2203. wmi_handle->tgt_force_assert_enable = val;
  2204. }
  2205. #ifndef CONFIG_MCL
  2206. /**
  2207. * API to flush all the previous packets associated with the wmi endpoint
  2208. *
  2209. * @param wmi_handle : handle to WMI.
  2210. */
  2211. void
  2212. wmi_flush_endpoint(wmi_unified_t wmi_handle)
  2213. {
  2214. htc_flush_endpoint(wmi_handle->htc_handle,
  2215. wmi_handle->wmi_endpoint_id, 0);
  2216. }
  2217. /**
  2218. * generic function to block unified WMI command
  2219. * @param wmi_handle : handle to WMI.
  2220. * @return 0 on success and -ve on failure.
  2221. */
  2222. int
  2223. wmi_stop(wmi_unified_t wmi_handle)
  2224. {
  2225. QDF_TRACE(QDF_MODULE_ID_WMI, QDF_TRACE_LEVEL_INFO,
  2226. "WMI Stop\n");
  2227. wmi_handle->wmi_stopinprogress = 1;
  2228. return 0;
  2229. }
  2230. /**
  2231. * wmi_pdev_id_conversion_enable() - API to enable pdev_id conversion in WMI
  2232. * By default pdev_id conversion is not done in WMI.
  2233. * This API can be used enable conversion in WMI.
  2234. * @param wmi_handle : handle to WMI
  2235. * Return none
  2236. */
  2237. void wmi_pdev_id_conversion_enable(wmi_unified_t wmi_handle)
  2238. {
  2239. if (wmi_handle->target_type == WMI_TLV_TARGET)
  2240. wmi_tlv_pdev_id_conversion_enable(wmi_handle);
  2241. }
  2242. #endif