wmi_tlv_helper.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /*
  2. * Copyright (c) 2013-2019 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for
  6. * any purpose with or without fee is hereby granted, provided that the
  7. * above copyright notice and this permission notice appear in all
  8. * copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  13. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  14. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  15. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. * PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #include "wmi_tlv_platform.c"
  20. #include "wmi_tlv_defs.h"
  21. #include "wmi_version.h"
  22. #include "qdf_module.h"
  23. #define WMITLV_GET_ATTRIB_NUM_TLVS 0xFFFFFFFF
  24. #define WMITLV_GET_CMDID(val) (val & 0x00FFFFFF)
  25. #define WMITLV_GET_NUM_TLVS(val) ((val >> 24) & 0xFF)
  26. #define WMITLV_GET_TAGID(val) (val & 0x00000FFF)
  27. #define WMITLV_GET_TAG_STRUCT_SIZE(val) ((val >> 12) & 0x000001FF)
  28. #define WMITLV_GET_TAG_ARRAY_SIZE(val) ((val >> 21) & 0x000001FF)
  29. #define WMITLV_GET_TAG_VARIED(val) ((val >> 30) & 0x00000001)
  30. #define WMITLV_SET_ATTRB0(id) ((WMITLV_GET_TAG_NUM_TLV_ATTRIB(id) << 24) | \
  31. (id & 0x00FFFFFF))
  32. #define WMITLV_SET_ATTRB1(tagID, tagStructSize, tagArraySize, tagVaried) \
  33. (((tagVaried&0x1)<<30) | ((tagArraySize&0x1FF)<<21) | \
  34. ((tagStructSize&0x1FF)<<12) | (tagID&0xFFF))
  35. #define WMITLV_OP_SET_TLV_ATTRIB_macro(param_ptr, param_len, wmi_cmd_event_id, \
  36. elem_tlv_tag, elem_struc_type, elem_name, var_len, arr_size) \
  37. WMITLV_SET_ATTRB1(elem_tlv_tag, sizeof(elem_struc_type), arr_size, var_len),
  38. #define WMITLV_GET_CMD_EVT_ATTRB_LIST(id) \
  39. WMITLV_SET_ATTRB0(id), \
  40. WMITLV_TABLE(id,SET_TLV_ATTRIB, NULL, 0)
  41. uint32_t cmd_attr_list[] = {
  42. WMITLV_ALL_CMD_LIST(WMITLV_GET_CMD_EVT_ATTRB_LIST)
  43. };
  44. uint32_t evt_attr_list[] = {
  45. WMITLV_ALL_EVT_LIST(WMITLV_GET_CMD_EVT_ATTRB_LIST)
  46. };
  47. #ifdef NO_DYNAMIC_MEM_ALLOC
  48. static wmitlv_cmd_param_info *g_wmi_static_cmd_param_info_buf;
  49. uint32_t g_wmi_static_max_cmd_param_tlvs;
  50. #endif
  51. /**
  52. * wmitlv_set_static_param_tlv_buf() - tlv helper function
  53. * @param_tlv_buf: tlv buffer parameter
  54. * @max_tlvs_accommodated: max no of tlv entries
  55. *
  56. *
  57. * WMI TLV Helper function to set the static cmd_param_tlv structure
  58. * and number of TLVs that can be accommodated in the structure.
  59. * This function should be used when dynamic memory allocation is not
  60. * supported. When dynamic memory allocation is not supported by any
  61. * component then NO_DYNAMIC_MEMALLOC macro has to be defined in respective
  62. * tlv_platform.c file. And respective component has to allocate
  63. * cmd_param_tlv structure buffer to accommodate whatever number of TLV's.
  64. * Both the buffer address and number of TLV's that can be accommodated in
  65. * the buffer should be sent as arguments to this function.
  66. *
  67. * Return None
  68. */
  69. void
  70. wmitlv_set_static_param_tlv_buf(void *param_tlv_buf,
  71. uint32_t max_tlvs_accommodated)
  72. {
  73. #ifdef NO_DYNAMIC_MEM_ALLOC
  74. g_wmi_static_cmd_param_info_buf = param_tlv_buf;
  75. g_wmi_static_max_cmd_param_tlvs = max_tlvs_accommodated;
  76. #endif
  77. }
  78. /**
  79. * wmitlv_get_attributes() - tlv helper function
  80. * @is_cmd_id: boolean for command attribute
  81. * @cmd_event_id: command event id
  82. * @curr_tlv_order: tlv order
  83. * @tlv_attr_ptr: pointer to tlv attribute
  84. *
  85. *
  86. * WMI TLV Helper functions to find the attributes of the
  87. * Command/Event TLVs.
  88. *
  89. * Return: 0 if success. Return >=1 if failure.
  90. */
  91. static
  92. uint32_t wmitlv_get_attributes(uint32_t is_cmd_id, uint32_t cmd_event_id,
  93. uint32_t curr_tlv_order,
  94. wmitlv_attributes_struc *tlv_attr_ptr)
  95. {
  96. uint32_t i, base_index, num_tlvs, num_entries;
  97. uint32_t *pAttrArrayList;
  98. if (is_cmd_id) {
  99. pAttrArrayList = &cmd_attr_list[0];
  100. num_entries = QDF_ARRAY_SIZE(cmd_attr_list);
  101. } else {
  102. pAttrArrayList = &evt_attr_list[0];
  103. num_entries = QDF_ARRAY_SIZE(evt_attr_list);
  104. }
  105. for (i = 0; i < num_entries; i++) {
  106. num_tlvs = WMITLV_GET_NUM_TLVS(pAttrArrayList[i]);
  107. if (WMITLV_GET_CMDID(cmd_event_id) ==
  108. WMITLV_GET_CMDID(pAttrArrayList[i])) {
  109. tlv_attr_ptr->cmd_num_tlv = num_tlvs;
  110. /* Return success from here when only number of TLVS for
  111. * this command/event is required */
  112. if (curr_tlv_order == WMITLV_GET_ATTRIB_NUM_TLVS) {
  113. wmi_tlv_print_verbose
  114. ("%s: WMI TLV attribute definitions for %s:0x%x found; num_of_tlvs:%d\n",
  115. __func__, (is_cmd_id ? "Cmd" : "Evt"),
  116. cmd_event_id, num_tlvs);
  117. return 0;
  118. }
  119. /* Return failure if tlv_order is more than the expected
  120. * number of TLVs */
  121. if (curr_tlv_order >= num_tlvs) {
  122. wmi_tlv_print_error
  123. ("%s: ERROR: TLV order %d greater than num_of_tlvs:%d for %s:0x%x\n",
  124. __func__, curr_tlv_order, num_tlvs,
  125. (is_cmd_id ? "Cmd" : "Evt"), cmd_event_id);
  126. return 1;
  127. }
  128. base_index = i + 1; /* index to first TLV attributes */
  129. wmi_tlv_print_verbose
  130. ("%s: WMI TLV attributes for %s:0x%x tlv[%d]:0x%x\n",
  131. __func__, (is_cmd_id ? "Cmd" : "Evt"),
  132. cmd_event_id, curr_tlv_order,
  133. pAttrArrayList[(base_index + curr_tlv_order)]);
  134. tlv_attr_ptr->tag_order = curr_tlv_order;
  135. tlv_attr_ptr->tag_id =
  136. WMITLV_GET_TAGID(pAttrArrayList
  137. [(base_index + curr_tlv_order)]);
  138. tlv_attr_ptr->tag_struct_size =
  139. WMITLV_GET_TAG_STRUCT_SIZE(pAttrArrayList
  140. [(base_index +
  141. curr_tlv_order)]);
  142. tlv_attr_ptr->tag_varied_size =
  143. WMITLV_GET_TAG_VARIED(pAttrArrayList
  144. [(base_index +
  145. curr_tlv_order)]);
  146. tlv_attr_ptr->tag_array_size =
  147. WMITLV_GET_TAG_ARRAY_SIZE(pAttrArrayList
  148. [(base_index +
  149. curr_tlv_order)]);
  150. return 0;
  151. }
  152. i += num_tlvs;
  153. }
  154. wmi_tlv_print_error
  155. ("%s: ERROR: Didn't found WMI TLV attribute definitions for %s:0x%x\n",
  156. __func__, (is_cmd_id ? "Cmd" : "Evt"), cmd_event_id);
  157. return 1;
  158. }
  159. /**
  160. * wmitlv_check_tlv_params() - tlv helper function
  161. * @os_handle: os context handle
  162. * @param_struc_ptr: pointer to tlv structure
  163. * @is_cmd_id: boolean for command attribute
  164. * @wmi_cmd_event_id: command event id
  165. *
  166. *
  167. * Helper Function to vaidate the prepared TLV's for
  168. * an WMI event/command to be sent.
  169. *
  170. * Return: 0 if success. Return < 0 if failure.
  171. */
  172. static int
  173. wmitlv_check_tlv_params(void *os_handle, void *param_struc_ptr,
  174. uint32_t param_buf_len, uint32_t is_cmd_id,
  175. uint32_t wmi_cmd_event_id)
  176. {
  177. wmitlv_attributes_struc attr_struct_ptr;
  178. uint32_t buf_idx = 0;
  179. uint32_t tlv_index = 0;
  180. uint8_t *buf_ptr = (unsigned char *)param_struc_ptr;
  181. uint32_t expected_num_tlvs, expected_tlv_len;
  182. int32_t error = -1;
  183. /* Get the number of TLVs for this command/event */
  184. if (wmitlv_get_attributes
  185. (is_cmd_id, wmi_cmd_event_id, WMITLV_GET_ATTRIB_NUM_TLVS,
  186. &attr_struct_ptr) != 0) {
  187. wmi_tlv_print_error
  188. ("%s: ERROR: Couldn't get expected number of TLVs for Cmd=%d\n",
  189. __func__, wmi_cmd_event_id);
  190. goto Error_wmitlv_check_tlv_params;
  191. }
  192. /* NOTE: the returned number of TLVs is in "attr_struct_ptr.cmd_num_tlv" */
  193. expected_num_tlvs = attr_struct_ptr.cmd_num_tlv;
  194. while ((buf_idx + WMI_TLV_HDR_SIZE) <= param_buf_len) {
  195. uint32_t curr_tlv_tag =
  196. WMITLV_GET_TLVTAG(WMITLV_GET_HDR(buf_ptr));
  197. uint32_t curr_tlv_len =
  198. WMITLV_GET_TLVLEN(WMITLV_GET_HDR(buf_ptr));
  199. if ((buf_idx + WMI_TLV_HDR_SIZE + curr_tlv_len) > param_buf_len) {
  200. wmi_tlv_print_error
  201. ("%s: ERROR: Invalid TLV length for Cmd=%d Tag_order=%d buf_idx=%d Tag:%d Len:%d TotalLen:%d\n",
  202. __func__, wmi_cmd_event_id, tlv_index, buf_idx,
  203. curr_tlv_tag, curr_tlv_len, param_buf_len);
  204. goto Error_wmitlv_check_tlv_params;
  205. }
  206. /* Get the attributes of the TLV with the given order in "tlv_index" */
  207. wmi_tlv_OS_MEMZERO(&attr_struct_ptr,
  208. sizeof(wmitlv_attributes_struc));
  209. if (wmitlv_get_attributes
  210. (is_cmd_id, wmi_cmd_event_id, tlv_index,
  211. &attr_struct_ptr) != 0) {
  212. wmi_tlv_print_error
  213. ("%s: ERROR: No TLV attributes found for Cmd=%d Tag_order=%d\n",
  214. __func__, wmi_cmd_event_id, tlv_index);
  215. goto Error_wmitlv_check_tlv_params;
  216. }
  217. /* Found the TLV that we wanted */
  218. wmi_tlv_print_verbose("%s: [tlv %d]: tag=%d, len=%d\n",
  219. __func__, tlv_index, curr_tlv_tag,
  220. curr_tlv_len);
  221. /* Validating Tag ID order */
  222. if (curr_tlv_tag != attr_struct_ptr.tag_id) {
  223. wmi_tlv_print_error
  224. ("%s: ERROR: TLV has wrong tag in order for Cmd=0x%x. Given=%d, Expected=%d.\n",
  225. __func__, wmi_cmd_event_id, curr_tlv_tag,
  226. attr_struct_ptr.tag_id);
  227. goto Error_wmitlv_check_tlv_params;
  228. }
  229. /* Validate Tag length */
  230. /* Array TLVs length checking needs special handling */
  231. if ((curr_tlv_tag >= WMITLV_TAG_FIRST_ARRAY_ENUM)
  232. && (curr_tlv_tag <= WMITLV_TAG_LAST_ARRAY_ENUM)) {
  233. if (attr_struct_ptr.tag_varied_size == WMITLV_SIZE_FIX) {
  234. /* Array size can't be invalid for fixed size Array TLV */
  235. if (WMITLV_ARR_SIZE_INVALID ==
  236. attr_struct_ptr.tag_array_size) {
  237. wmi_tlv_print_error
  238. ("%s: ERROR: array_size can't be invalid for Array TLV Cmd=0x%x Tag=%d\n",
  239. __func__, wmi_cmd_event_id,
  240. curr_tlv_tag);
  241. goto Error_wmitlv_check_tlv_params;
  242. }
  243. expected_tlv_len =
  244. attr_struct_ptr.tag_array_size *
  245. attr_struct_ptr.tag_struct_size;
  246. /* Paddding is only required for Byte array Tlvs all other
  247. * array tlv's should be aligned to 4 bytes during their
  248. * definition */
  249. if (WMITLV_TAG_ARRAY_BYTE ==
  250. attr_struct_ptr.tag_id) {
  251. expected_tlv_len =
  252. roundup(expected_tlv_len,
  253. sizeof(uint32_t));
  254. }
  255. if (curr_tlv_len != expected_tlv_len) {
  256. wmi_tlv_print_error
  257. ("%s: ERROR: TLV has wrong length for Cmd=0x%x. Tag_order=%d Tag=%d, Given_Len:%d Expected_Len=%d.\n",
  258. __func__, wmi_cmd_event_id,
  259. tlv_index, curr_tlv_tag,
  260. curr_tlv_len, expected_tlv_len);
  261. goto Error_wmitlv_check_tlv_params;
  262. }
  263. } else {
  264. /* Array size should be invalid for variable size Array TLV */
  265. if (WMITLV_ARR_SIZE_INVALID !=
  266. attr_struct_ptr.tag_array_size) {
  267. wmi_tlv_print_error
  268. ("%s: ERROR: array_size should be invalid for Array TLV Cmd=0x%x Tag=%d\n",
  269. __func__, wmi_cmd_event_id,
  270. curr_tlv_tag);
  271. goto Error_wmitlv_check_tlv_params;
  272. }
  273. /* Incase of variable length TLV's, there is no expectation
  274. * on the length field so do whatever checking you can
  275. * depending on the TLV tag if TLV length is non-zero */
  276. if (curr_tlv_len != 0) {
  277. /* Verify TLV length is aligned to the size of structure */
  278. if ((curr_tlv_len %
  279. attr_struct_ptr.tag_struct_size) !=
  280. 0) {
  281. wmi_tlv_print_error
  282. ("%s: ERROR: TLV length %d for Cmd=0x%x is not aligned to size of structure(%d bytes)\n",
  283. __func__, curr_tlv_len,
  284. wmi_cmd_event_id,
  285. attr_struct_ptr.
  286. tag_struct_size);
  287. goto Error_wmitlv_check_tlv_params;
  288. }
  289. if (curr_tlv_tag ==
  290. WMITLV_TAG_ARRAY_STRUC) {
  291. uint8_t *tlv_buf_ptr = NULL;
  292. uint32_t in_tlv_len;
  293. uint32_t idx;
  294. uint32_t num_of_elems;
  295. /* Verify length of inner TLVs */
  296. num_of_elems =
  297. curr_tlv_len /
  298. attr_struct_ptr.
  299. tag_struct_size;
  300. /* Set tlv_buf_ptr to the first inner TLV address */
  301. tlv_buf_ptr =
  302. buf_ptr + WMI_TLV_HDR_SIZE;
  303. for (idx = 0;
  304. idx < num_of_elems;
  305. idx++) {
  306. in_tlv_len =
  307. WMITLV_GET_TLVLEN
  308. (WMITLV_GET_HDR
  309. (tlv_buf_ptr));
  310. if ((in_tlv_len +
  311. WMI_TLV_HDR_SIZE)
  312. !=
  313. attr_struct_ptr.
  314. tag_struct_size) {
  315. wmi_tlv_print_error
  316. ("%s: ERROR: TLV has wrong length for Cmd=0x%x. Tag_order=%d Tag=%d, Given_Len:%zu Expected_Len=%d.\n",
  317. __func__,
  318. wmi_cmd_event_id,
  319. tlv_index,
  320. curr_tlv_tag,
  321. (in_tlv_len
  322. +
  323. WMI_TLV_HDR_SIZE),
  324. attr_struct_ptr.
  325. tag_struct_size);
  326. goto Error_wmitlv_check_tlv_params;
  327. }
  328. tlv_buf_ptr +=
  329. in_tlv_len +
  330. WMI_TLV_HDR_SIZE;
  331. }
  332. } else
  333. if ((curr_tlv_tag ==
  334. WMITLV_TAG_ARRAY_UINT32)
  335. || (curr_tlv_tag ==
  336. WMITLV_TAG_ARRAY_BYTE)
  337. || (curr_tlv_tag ==
  338. WMITLV_TAG_ARRAY_FIXED_STRUC)) {
  339. /* Nothing to verify here */
  340. } else {
  341. wmi_tlv_print_error
  342. ("%s ERROR Need to handle the Array tlv %d for variable length for Cmd=0x%x\n",
  343. __func__,
  344. attr_struct_ptr.tag_id,
  345. wmi_cmd_event_id);
  346. goto Error_wmitlv_check_tlv_params;
  347. }
  348. }
  349. }
  350. } else {
  351. /* Non-array TLV. */
  352. if ((curr_tlv_len + WMI_TLV_HDR_SIZE) !=
  353. attr_struct_ptr.tag_struct_size) {
  354. wmi_tlv_print_error
  355. ("%s: ERROR: TLV has wrong length for Cmd=0x%x. Given=%zu, Expected=%d.\n",
  356. __func__, wmi_cmd_event_id,
  357. (curr_tlv_len + WMI_TLV_HDR_SIZE),
  358. attr_struct_ptr.tag_struct_size);
  359. goto Error_wmitlv_check_tlv_params;
  360. }
  361. }
  362. /* Check TLV length is aligned to 4 bytes or not */
  363. if ((curr_tlv_len % sizeof(uint32_t)) != 0) {
  364. wmi_tlv_print_error
  365. ("%s: ERROR: TLV length %d for Cmd=0x%x is not aligned to %zu bytes\n",
  366. __func__, curr_tlv_len, wmi_cmd_event_id,
  367. sizeof(uint32_t));
  368. goto Error_wmitlv_check_tlv_params;
  369. }
  370. tlv_index++;
  371. buf_ptr += curr_tlv_len + WMI_TLV_HDR_SIZE;
  372. buf_idx += curr_tlv_len + WMI_TLV_HDR_SIZE;
  373. }
  374. if (tlv_index != expected_num_tlvs) {
  375. wmi_tlv_print_verbose
  376. ("%s: INFO: Less number of TLVs filled for Cmd=0x%x Filled %d Expected=%d\n",
  377. __func__, wmi_cmd_event_id, tlv_index, expected_num_tlvs);
  378. }
  379. return 0;
  380. Error_wmitlv_check_tlv_params:
  381. return error;
  382. }
  383. /**
  384. * wmitlv_check_event_tlv_params() - tlv helper function
  385. * @os_handle: os context handle
  386. * @param_struc_ptr: pointer to tlv structure
  387. * @is_cmd_id: boolean for command attribute
  388. * @wmi_cmd_event_id: command event id
  389. *
  390. *
  391. * Helper Function to vaidate the prepared TLV's for
  392. * an WMI event/command to be sent.
  393. *
  394. * Return: 0 if success. Return < 0 if failure.
  395. */
  396. int
  397. wmitlv_check_event_tlv_params(void *os_handle, void *param_struc_ptr,
  398. uint32_t param_buf_len, uint32_t wmi_cmd_event_id)
  399. {
  400. uint32_t is_cmd_id = 0;
  401. return wmitlv_check_tlv_params
  402. (os_handle, param_struc_ptr, param_buf_len, is_cmd_id,
  403. wmi_cmd_event_id);
  404. }
  405. /**
  406. * wmitlv_check_command_tlv_params() - tlv helper function
  407. * @os_handle: os context handle
  408. * @param_struc_ptr: pointer to tlv structure
  409. * @is_cmd_id: boolean for command attribute
  410. * @wmi_cmd_event_id: command event id
  411. *
  412. *
  413. * Helper Function to vaidate the prepared TLV's for
  414. * an WMI event/command to be sent.
  415. *
  416. * Return: 0 if success. Return < 0 if failure.
  417. */
  418. int
  419. wmitlv_check_command_tlv_params(void *os_handle, void *param_struc_ptr,
  420. uint32_t param_buf_len,
  421. uint32_t wmi_cmd_event_id)
  422. {
  423. uint32_t is_cmd_id = 1;
  424. return wmitlv_check_tlv_params
  425. (os_handle, param_struc_ptr, param_buf_len, is_cmd_id,
  426. wmi_cmd_event_id);
  427. }
  428. qdf_export_symbol(wmitlv_check_command_tlv_params);
  429. /**
  430. * wmitlv_check_and_pad_tlvs() - tlv helper function
  431. * @os_handle: os context handle
  432. * @param_buf_len: length of tlv parameter
  433. * @param_struc_ptr: pointer to tlv structure
  434. * @is_cmd_id: boolean for command attribute
  435. * @wmi_cmd_event_id: command event id
  436. * @wmi_cmd_struct_ptr: wmi command structure
  437. *
  438. *
  439. * vaidate the TLV's coming for an event/command and
  440. * also pads data to TLV's if necessary
  441. *
  442. * Return: 0 if success. Return < 0 if failure.
  443. */
  444. static int
  445. wmitlv_check_and_pad_tlvs(void *os_handle, void *param_struc_ptr,
  446. uint32_t param_buf_len, uint32_t is_cmd_id,
  447. uint32_t wmi_cmd_event_id, void **wmi_cmd_struct_ptr)
  448. {
  449. wmitlv_attributes_struc attr_struct_ptr;
  450. uint32_t buf_idx = 0;
  451. uint32_t tlv_index = 0;
  452. uint32_t num_of_elems = 0;
  453. int tlv_size_diff = 0;
  454. uint8_t *buf_ptr = (unsigned char *)param_struc_ptr;
  455. wmitlv_cmd_param_info *cmd_param_tlvs_ptr = NULL;
  456. uint32_t remaining_expected_tlvs = 0xFFFFFFFF;
  457. uint32_t len_wmi_cmd_struct_buf;
  458. uint32_t free_buf_len;
  459. int32_t error = -1;
  460. /* Get the number of TLVs for this command/event */
  461. if (wmitlv_get_attributes
  462. (is_cmd_id, wmi_cmd_event_id, WMITLV_GET_ATTRIB_NUM_TLVS,
  463. &attr_struct_ptr) != 0) {
  464. wmi_tlv_print_error
  465. ("%s: ERROR: Couldn't get expected number of TLVs for Cmd=%d\n",
  466. __func__, wmi_cmd_event_id);
  467. return error;
  468. }
  469. /* NOTE: the returned number of TLVs is in "attr_struct_ptr.cmd_num_tlv" */
  470. if (param_buf_len < WMI_TLV_HDR_SIZE) {
  471. wmi_tlv_print_error
  472. ("%s: ERROR: Incorrect param buf length passed\n",
  473. __func__);
  474. return error;
  475. }
  476. /* Create base structure of format wmi_cmd_event_id##_param_tlvs */
  477. len_wmi_cmd_struct_buf =
  478. attr_struct_ptr.cmd_num_tlv * sizeof(wmitlv_cmd_param_info);
  479. #ifndef NO_DYNAMIC_MEM_ALLOC
  480. /* Dynamic memory allocation supported */
  481. wmi_tlv_os_mem_alloc(os_handle, *wmi_cmd_struct_ptr,
  482. len_wmi_cmd_struct_buf);
  483. #else
  484. /* Dynamic memory allocation is not supported. Use the buffer
  485. * g_wmi_static_cmd_param_info_buf, which should be set using
  486. * wmi_tlv_set_static_param_tlv_buf(),
  487. * for base structure of format wmi_cmd_event_id##_param_tlvs */
  488. *wmi_cmd_struct_ptr = g_wmi_static_cmd_param_info_buf;
  489. if (attr_struct_ptr.cmd_num_tlv > g_wmi_static_max_cmd_param_tlvs) {
  490. /* Error: Expecting more TLVs that accommodated for static structure */
  491. wmi_tlv_print_error
  492. ("%s: Error: Expecting more TLVs that accommodated for static structure. Expected:%d Accomodated:%d\n",
  493. __func__, attr_struct_ptr.cmd_num_tlv,
  494. g_wmi_static_max_cmd_param_tlvs);
  495. return error;
  496. }
  497. #endif
  498. if (!*wmi_cmd_struct_ptr) {
  499. /* Error: unable to alloc memory */
  500. wmi_tlv_print_error
  501. ("%s: Error: unable to alloc memory (size=%d) for TLV\n",
  502. __func__, len_wmi_cmd_struct_buf);
  503. return error;
  504. }
  505. cmd_param_tlvs_ptr = (wmitlv_cmd_param_info *) *wmi_cmd_struct_ptr;
  506. wmi_tlv_OS_MEMZERO(cmd_param_tlvs_ptr, len_wmi_cmd_struct_buf);
  507. remaining_expected_tlvs = attr_struct_ptr.cmd_num_tlv;
  508. while (((buf_idx + WMI_TLV_HDR_SIZE) <= param_buf_len)
  509. && (remaining_expected_tlvs)) {
  510. uint32_t curr_tlv_tag =
  511. WMITLV_GET_TLVTAG(WMITLV_GET_HDR(buf_ptr));
  512. uint32_t curr_tlv_len =
  513. WMITLV_GET_TLVLEN(WMITLV_GET_HDR(buf_ptr));
  514. int num_padding_bytes = 0;
  515. free_buf_len = param_buf_len - (buf_idx + WMI_TLV_HDR_SIZE);
  516. if (curr_tlv_len > free_buf_len) {
  517. wmi_tlv_print_error("%s: TLV length overflow",
  518. __func__);
  519. goto Error_wmitlv_check_and_pad_tlvs;
  520. }
  521. /* Get the attributes of the TLV with the given order in "tlv_index" */
  522. wmi_tlv_OS_MEMZERO(&attr_struct_ptr,
  523. sizeof(wmitlv_attributes_struc));
  524. if (wmitlv_get_attributes
  525. (is_cmd_id, wmi_cmd_event_id, tlv_index,
  526. &attr_struct_ptr) != 0) {
  527. wmi_tlv_print_error
  528. ("%s: ERROR: No TLV attributes found for Cmd=%d Tag_order=%d\n",
  529. __func__, wmi_cmd_event_id, tlv_index);
  530. goto Error_wmitlv_check_and_pad_tlvs;
  531. }
  532. /* Found the TLV that we wanted */
  533. wmi_tlv_print_verbose("%s: [tlv %d]: tag=%d, len=%d\n",
  534. __func__, tlv_index, curr_tlv_tag,
  535. curr_tlv_len);
  536. /* Validating Tag order */
  537. if (curr_tlv_tag != attr_struct_ptr.tag_id) {
  538. wmi_tlv_print_error
  539. ("%s: ERROR: TLV has wrong tag in order for Cmd=0x%x. Given=%d, Expected=%d, total_tlv=%d, remaining tlv=%d.\n",
  540. __func__, wmi_cmd_event_id, curr_tlv_tag,
  541. attr_struct_ptr.tag_id,
  542. attr_struct_ptr.cmd_num_tlv,
  543. remaining_expected_tlvs);
  544. goto Error_wmitlv_check_and_pad_tlvs;
  545. }
  546. if ((curr_tlv_tag >= WMITLV_TAG_FIRST_ARRAY_ENUM)
  547. && (curr_tlv_tag <= WMITLV_TAG_LAST_ARRAY_ENUM)) {
  548. /* Current Tag is an array of some kind. */
  549. /* Skip the TLV header of this array */
  550. buf_ptr += WMI_TLV_HDR_SIZE;
  551. buf_idx += WMI_TLV_HDR_SIZE;
  552. } else {
  553. /* Non-array TLV. */
  554. curr_tlv_len += WMI_TLV_HDR_SIZE;
  555. }
  556. if (attr_struct_ptr.tag_varied_size == WMITLV_SIZE_FIX) {
  557. /* This TLV is fixed length */
  558. if (WMITLV_ARR_SIZE_INVALID ==
  559. attr_struct_ptr.tag_array_size) {
  560. tlv_size_diff =
  561. curr_tlv_len -
  562. attr_struct_ptr.tag_struct_size;
  563. num_of_elems =
  564. (curr_tlv_len > WMI_TLV_HDR_SIZE) ? 1 : 0;
  565. } else {
  566. tlv_size_diff =
  567. curr_tlv_len -
  568. (attr_struct_ptr.tag_struct_size *
  569. attr_struct_ptr.tag_array_size);
  570. num_of_elems = attr_struct_ptr.tag_array_size;
  571. }
  572. } else {
  573. /* This TLV has a variable number of elements */
  574. if (WMITLV_TAG_ARRAY_STRUC == attr_struct_ptr.tag_id) {
  575. uint32_t in_tlv_len = 0;
  576. if (curr_tlv_len != 0) {
  577. in_tlv_len =
  578. WMITLV_GET_TLVLEN(WMITLV_GET_HDR
  579. (buf_ptr));
  580. in_tlv_len += WMI_TLV_HDR_SIZE;
  581. if (in_tlv_len > curr_tlv_len) {
  582. wmi_tlv_print_error("%s: Invalid in_tlv_len=%d",
  583. __func__,
  584. in_tlv_len);
  585. goto
  586. Error_wmitlv_check_and_pad_tlvs;
  587. }
  588. tlv_size_diff =
  589. in_tlv_len -
  590. attr_struct_ptr.tag_struct_size;
  591. num_of_elems =
  592. curr_tlv_len / in_tlv_len;
  593. wmi_tlv_print_verbose
  594. ("%s: WARN: TLV array of structures in_tlv_len=%d struct_size:%d diff:%d num_of_elems=%d \n",
  595. __func__, in_tlv_len,
  596. attr_struct_ptr.tag_struct_size,
  597. tlv_size_diff, num_of_elems);
  598. } else {
  599. tlv_size_diff = 0;
  600. num_of_elems = 0;
  601. }
  602. } else
  603. if ((WMITLV_TAG_ARRAY_UINT32 ==
  604. attr_struct_ptr.tag_id)
  605. || (WMITLV_TAG_ARRAY_BYTE ==
  606. attr_struct_ptr.tag_id)
  607. || (WMITLV_TAG_ARRAY_FIXED_STRUC ==
  608. attr_struct_ptr.tag_id)) {
  609. tlv_size_diff = 0;
  610. num_of_elems =
  611. curr_tlv_len /
  612. attr_struct_ptr.tag_struct_size;
  613. } else {
  614. wmi_tlv_print_error
  615. ("%s ERROR Need to handle this tag ID for variable length %d\n",
  616. __func__, attr_struct_ptr.tag_id);
  617. goto Error_wmitlv_check_and_pad_tlvs;
  618. }
  619. }
  620. if ((WMITLV_TAG_ARRAY_STRUC == attr_struct_ptr.tag_id) &&
  621. (tlv_size_diff != 0)) {
  622. void *new_tlv_buf = NULL;
  623. uint8_t *tlv_buf_ptr = NULL;
  624. uint32_t in_tlv_len;
  625. uint32_t i;
  626. if (attr_struct_ptr.tag_varied_size == WMITLV_SIZE_FIX) {
  627. /* This is not allowed. The tag WMITLV_TAG_ARRAY_STRUC can
  628. * only be used with variable-length structure array
  629. * should not have a fixed number of elements (contradicting).
  630. * Use WMITLV_TAG_ARRAY_FIXED_STRUC tag for fixed size
  631. * structure array(where structure never change without
  632. * breaking compatibility) */
  633. wmi_tlv_print_error
  634. ("%s: ERROR: TLV (tag=%d) should be variable-length and not fixed length\n",
  635. __func__, curr_tlv_tag);
  636. goto Error_wmitlv_check_and_pad_tlvs;
  637. }
  638. /* Warning: Needs to allocate a larger structure and pad with zeros */
  639. wmi_tlv_print_verbose
  640. ("%s: WARN: TLV array of structures needs padding. tlv_size_diff=%d\n",
  641. __func__, tlv_size_diff);
  642. /* incoming structure length */
  643. in_tlv_len =
  644. WMITLV_GET_TLVLEN(WMITLV_GET_HDR(buf_ptr)) +
  645. WMI_TLV_HDR_SIZE;
  646. #ifndef NO_DYNAMIC_MEM_ALLOC
  647. wmi_tlv_os_mem_alloc(os_handle, new_tlv_buf,
  648. (num_of_elems *
  649. attr_struct_ptr.tag_struct_size));
  650. if (!new_tlv_buf) {
  651. /* Error: unable to alloc memory */
  652. wmi_tlv_print_error
  653. ("%s: Error: unable to alloc memory (size=%d) for padding the TLV array %d\n",
  654. __func__,
  655. (num_of_elems *
  656. attr_struct_ptr.tag_struct_size),
  657. curr_tlv_tag);
  658. goto Error_wmitlv_check_and_pad_tlvs;
  659. }
  660. wmi_tlv_OS_MEMZERO(new_tlv_buf,
  661. (num_of_elems *
  662. attr_struct_ptr.tag_struct_size));
  663. tlv_buf_ptr = (uint8_t *) new_tlv_buf;
  664. for (i = 0; i < num_of_elems; i++) {
  665. if (tlv_size_diff > 0) {
  666. /* Incoming structure size is greater than expected
  667. * structure size. so copy the number of bytes equal
  668. * to expected structure size */
  669. wmi_tlv_OS_MEMCPY(tlv_buf_ptr,
  670. (void *)(buf_ptr +
  671. i *
  672. in_tlv_len),
  673. attr_struct_ptr.
  674. tag_struct_size);
  675. } else {
  676. /* Incoming structure size is smaller than expected
  677. * structure size. so copy the number of bytes equal
  678. * to incoming structure size */
  679. wmi_tlv_OS_MEMCPY(tlv_buf_ptr,
  680. (void *)(buf_ptr +
  681. i *
  682. in_tlv_len),
  683. in_tlv_len);
  684. }
  685. tlv_buf_ptr += attr_struct_ptr.tag_struct_size;
  686. }
  687. #else
  688. {
  689. uint8_t *src_addr;
  690. uint8_t *dst_addr;
  691. uint32_t buf_mov_len;
  692. if (tlv_size_diff < 0) {
  693. /* Incoming structure size is smaller than expected size
  694. * then this needs padding for each element in the array */
  695. /* Find amount of bytes to be padded for one element */
  696. num_padding_bytes = tlv_size_diff * -1;
  697. /* Move subsequent TLVs by number of bytes to be padded
  698. * for all elements */
  699. if ((free_buf_len <
  700. attr_struct_ptr.tag_struct_size *
  701. num_of_elems) ||
  702. (param_buf_len <
  703. buf_idx + curr_tlv_len +
  704. num_padding_bytes * num_of_elems)) {
  705. wmi_tlv_print_error("%s: Insufficent buffer\n",
  706. __func__);
  707. goto
  708. Error_wmitlv_check_and_pad_tlvs;
  709. } else {
  710. src_addr =
  711. buf_ptr + curr_tlv_len;
  712. dst_addr =
  713. buf_ptr + curr_tlv_len +
  714. (num_padding_bytes *
  715. num_of_elems);
  716. buf_mov_len =
  717. param_buf_len - (buf_idx +
  718. curr_tlv_len);
  719. wmi_tlv_OS_MEMMOVE(dst_addr,
  720. src_addr,
  721. buf_mov_len);
  722. }
  723. /* Move subsequent elements of array down by number of
  724. * bytes to be padded for one element and alse set
  725. * padding bytes to zero */
  726. tlv_buf_ptr = buf_ptr;
  727. for (i = 0; i < num_of_elems - 1; i++) {
  728. src_addr =
  729. tlv_buf_ptr + in_tlv_len;
  730. if (i != (num_of_elems - 1)) {
  731. dst_addr =
  732. tlv_buf_ptr +
  733. in_tlv_len +
  734. num_padding_bytes;
  735. buf_mov_len =
  736. curr_tlv_len -
  737. ((i +
  738. 1) * in_tlv_len);
  739. wmi_tlv_OS_MEMMOVE
  740. (dst_addr, src_addr,
  741. buf_mov_len);
  742. }
  743. /* Set the padding bytes to zeroes */
  744. wmi_tlv_OS_MEMZERO(src_addr,
  745. num_padding_bytes);
  746. tlv_buf_ptr +=
  747. attr_struct_ptr.
  748. tag_struct_size;
  749. }
  750. src_addr = tlv_buf_ptr + in_tlv_len;
  751. wmi_tlv_OS_MEMZERO(src_addr,
  752. num_padding_bytes);
  753. /* Update the number of padding bytes to total number
  754. * of bytes padded for all elements in the array */
  755. num_padding_bytes =
  756. num_padding_bytes * num_of_elems;
  757. new_tlv_buf = buf_ptr;
  758. } else {
  759. /* Incoming structure size is greater than expected size
  760. * then this needs shrinking for each element in the array */
  761. /* Find amount of bytes to be shrunk for one element */
  762. num_padding_bytes = tlv_size_diff * -1;
  763. /* Move subsequent elements of array up by number of bytes
  764. * to be shrunk for one element */
  765. tlv_buf_ptr = buf_ptr;
  766. for (i = 0; i < (num_of_elems - 1); i++) {
  767. src_addr =
  768. tlv_buf_ptr + in_tlv_len;
  769. dst_addr =
  770. tlv_buf_ptr + in_tlv_len +
  771. num_padding_bytes;
  772. buf_mov_len =
  773. curr_tlv_len -
  774. ((i + 1) * in_tlv_len);
  775. wmi_tlv_OS_MEMMOVE(dst_addr,
  776. src_addr,
  777. buf_mov_len);
  778. tlv_buf_ptr +=
  779. attr_struct_ptr.
  780. tag_struct_size;
  781. }
  782. /* Move subsequent TLVs by number of bytes to be shrunk
  783. * for all elements */
  784. if (param_buf_len >
  785. (buf_idx + curr_tlv_len)) {
  786. src_addr =
  787. buf_ptr + curr_tlv_len;
  788. dst_addr =
  789. buf_ptr + curr_tlv_len +
  790. (num_padding_bytes *
  791. num_of_elems);
  792. buf_mov_len =
  793. param_buf_len - (buf_idx +
  794. curr_tlv_len);
  795. wmi_tlv_OS_MEMMOVE(dst_addr,
  796. src_addr,
  797. buf_mov_len);
  798. }
  799. /* Update the number of padding bytes to total number of
  800. * bytes shrunk for all elements in the array */
  801. num_padding_bytes =
  802. num_padding_bytes * num_of_elems;
  803. new_tlv_buf = buf_ptr;
  804. }
  805. }
  806. #endif
  807. cmd_param_tlvs_ptr[tlv_index].tlv_ptr = new_tlv_buf;
  808. cmd_param_tlvs_ptr[tlv_index].num_elements =
  809. num_of_elems;
  810. cmd_param_tlvs_ptr[tlv_index].buf_is_allocated = 1; /* Indicates that buffer is allocated */
  811. } else if (tlv_size_diff >= 0) {
  812. /* Warning: some parameter truncation */
  813. if (tlv_size_diff > 0) {
  814. wmi_tlv_print_verbose
  815. ("%s: WARN: TLV truncated. tlv_size_diff=%d, curr_tlv_len=%d\n",
  816. __func__, tlv_size_diff, curr_tlv_len);
  817. }
  818. /* TODO: this next line needs more comments and explanation */
  819. cmd_param_tlvs_ptr[tlv_index].tlv_ptr =
  820. (attr_struct_ptr.tag_varied_size
  821. && !curr_tlv_len) ? NULL : (void *)buf_ptr;
  822. cmd_param_tlvs_ptr[tlv_index].num_elements =
  823. num_of_elems;
  824. cmd_param_tlvs_ptr[tlv_index].buf_is_allocated = 0; /* Indicates that buffer is not allocated */
  825. } else {
  826. void *new_tlv_buf = NULL;
  827. /* Warning: Needs to allocate a larger structure and pad with zeros */
  828. wmi_tlv_print_verbose
  829. ("%s: WARN: TLV needs padding. tlv_size_diff=%d\n",
  830. __func__, tlv_size_diff);
  831. #ifndef NO_DYNAMIC_MEM_ALLOC
  832. /* Dynamic memory allocation is supported */
  833. wmi_tlv_os_mem_alloc(os_handle, new_tlv_buf,
  834. (curr_tlv_len - tlv_size_diff));
  835. if (!new_tlv_buf) {
  836. /* Error: unable to alloc memory */
  837. wmi_tlv_print_error
  838. ("%s: Error: unable to alloc memory (size=%d) for padding the TLV %d\n",
  839. __func__, (curr_tlv_len - tlv_size_diff),
  840. curr_tlv_tag);
  841. goto Error_wmitlv_check_and_pad_tlvs;
  842. }
  843. wmi_tlv_OS_MEMZERO(new_tlv_buf,
  844. (curr_tlv_len - tlv_size_diff));
  845. wmi_tlv_OS_MEMCPY(new_tlv_buf, (void *)buf_ptr,
  846. curr_tlv_len);
  847. #else
  848. /* Dynamic memory allocation is not supported. Padding has
  849. * to be done with in the existing buffer assuming we have
  850. * enough space to grow */
  851. {
  852. /* Note: tlv_size_diff is a value less than zero */
  853. /* Move the Subsequent TLVs by amount of bytes needs to be padded */
  854. uint8_t *src_addr;
  855. uint8_t *dst_addr;
  856. uint32_t src_len;
  857. num_padding_bytes = (tlv_size_diff * -1);
  858. src_addr = buf_ptr + curr_tlv_len;
  859. dst_addr =
  860. buf_ptr + curr_tlv_len + num_padding_bytes;
  861. src_len =
  862. param_buf_len - (buf_idx + curr_tlv_len);
  863. wmi_tlv_OS_MEMMOVE(dst_addr, src_addr, src_len);
  864. /* Set the padding bytes to zeroes */
  865. wmi_tlv_OS_MEMZERO(src_addr, num_padding_bytes);
  866. new_tlv_buf = buf_ptr;
  867. }
  868. #endif
  869. cmd_param_tlvs_ptr[tlv_index].tlv_ptr = new_tlv_buf;
  870. cmd_param_tlvs_ptr[tlv_index].num_elements =
  871. num_of_elems;
  872. cmd_param_tlvs_ptr[tlv_index].buf_is_allocated = 1; /* Indicates that buffer is allocated */
  873. }
  874. tlv_index++;
  875. remaining_expected_tlvs--;
  876. buf_ptr += curr_tlv_len + num_padding_bytes;
  877. buf_idx += curr_tlv_len + num_padding_bytes;
  878. }
  879. return 0;
  880. Error_wmitlv_check_and_pad_tlvs:
  881. if (is_cmd_id) {
  882. wmitlv_free_allocated_command_tlvs(wmi_cmd_event_id,
  883. wmi_cmd_struct_ptr);
  884. } else {
  885. wmitlv_free_allocated_event_tlvs(wmi_cmd_event_id,
  886. wmi_cmd_struct_ptr);
  887. }
  888. *wmi_cmd_struct_ptr = NULL;
  889. return error;
  890. }
  891. /**
  892. * wmitlv_check_and_pad_event_tlvs() - tlv helper function
  893. * @os_handle: os context handle
  894. * @param_struc_ptr: pointer to tlv structure
  895. * @param_buf_len: length of tlv parameter
  896. * @wmi_cmd_event_id: command event id
  897. * @wmi_cmd_struct_ptr: wmi command structure
  898. *
  899. *
  900. * validate and pad(if necessary) for incoming WMI Event TLVs
  901. *
  902. * Return: 0 if success. Return < 0 if failure.
  903. */
  904. int
  905. wmitlv_check_and_pad_event_tlvs(void *os_handle, void *param_struc_ptr,
  906. uint32_t param_buf_len,
  907. uint32_t wmi_cmd_event_id,
  908. void **wmi_cmd_struct_ptr)
  909. {
  910. uint32_t is_cmd_id = 0;
  911. return wmitlv_check_and_pad_tlvs
  912. (os_handle, param_struc_ptr, param_buf_len, is_cmd_id,
  913. wmi_cmd_event_id, wmi_cmd_struct_ptr);
  914. }
  915. qdf_export_symbol(wmitlv_check_and_pad_event_tlvs);
  916. /**
  917. * wmitlv_check_and_pad_command_tlvs() - tlv helper function
  918. * @os_handle: os context handle
  919. * @param_struc_ptr: pointer to tlv structure
  920. * @param_buf_len: length of tlv parameter
  921. * @wmi_cmd_event_id: command event id
  922. * @wmi_cmd_struct_ptr: wmi command structure
  923. *
  924. *
  925. * validate and pad(if necessary) for incoming WMI Command TLVs
  926. *
  927. * Return: 0 if success. Return < 0 if failure.
  928. */
  929. int
  930. wmitlv_check_and_pad_command_tlvs(void *os_handle, void *param_struc_ptr,
  931. uint32_t param_buf_len,
  932. uint32_t wmi_cmd_event_id,
  933. void **wmi_cmd_struct_ptr)
  934. {
  935. uint32_t is_cmd_id = 1;
  936. return wmitlv_check_and_pad_tlvs
  937. (os_handle, param_struc_ptr, param_buf_len, is_cmd_id,
  938. wmi_cmd_event_id, wmi_cmd_struct_ptr);
  939. }
  940. /**
  941. * wmitlv_free_allocated_tlvs() - tlv helper function
  942. * @is_cmd_id: bollean to check if cmd or event tlv
  943. * @cmd_event_id: command or event id
  944. * @wmi_cmd_struct_ptr: wmi command structure
  945. *
  946. *
  947. * free any allocated buffers for WMI Event/Command TLV processing
  948. *
  949. * Return: none
  950. */
  951. static void wmitlv_free_allocated_tlvs(uint32_t is_cmd_id,
  952. uint32_t cmd_event_id,
  953. void **wmi_cmd_struct_ptr)
  954. {
  955. void *ptr = *wmi_cmd_struct_ptr;
  956. if (!ptr) {
  957. wmi_tlv_print_error("%s: Nothing to free for CMD/Event 0x%x\n",
  958. __func__, cmd_event_id);
  959. return;
  960. }
  961. #ifndef NO_DYNAMIC_MEM_ALLOC
  962. /* macro to free that previously allocated memory for this TLV. When (op==FREE_TLV_ELEM). */
  963. #define WMITLV_OP_FREE_TLV_ELEM_macro(param_ptr, param_len, wmi_cmd_event_id, elem_tlv_tag, elem_struc_type, elem_name, var_len, arr_size) \
  964. if ((((WMITLV_TYPEDEF_STRUCT_PARAMS_TLVS(wmi_cmd_event_id) *)ptr)->WMITLV_FIELD_BUF_IS_ALLOCATED(elem_name)) && \
  965. (((WMITLV_TYPEDEF_STRUCT_PARAMS_TLVS(wmi_cmd_event_id) *)ptr)->elem_name)) \
  966. { \
  967. wmi_tlv_os_mem_free(((WMITLV_TYPEDEF_STRUCT_PARAMS_TLVS(wmi_cmd_event_id) *)ptr)->elem_name); \
  968. }
  969. #define WMITLV_FREE_TLV_ELEMS(id) \
  970. case id: \
  971. { \
  972. WMITLV_TABLE(id, FREE_TLV_ELEM, NULL, 0) \
  973. } \
  974. break;
  975. if (is_cmd_id) {
  976. switch (cmd_event_id) {
  977. WMITLV_ALL_CMD_LIST(WMITLV_FREE_TLV_ELEMS);
  978. default:
  979. wmi_tlv_print_error
  980. ("%s: ERROR: Cannot find the TLVs attributes for Cmd=0x%x, %d\n",
  981. __func__, cmd_event_id, cmd_event_id);
  982. }
  983. } else {
  984. switch (cmd_event_id) {
  985. WMITLV_ALL_EVT_LIST(WMITLV_FREE_TLV_ELEMS);
  986. default:
  987. wmi_tlv_print_error
  988. ("%s: ERROR: Cannot find the TLVs attributes for Cmd=0x%x, %d\n",
  989. __func__, cmd_event_id, cmd_event_id);
  990. }
  991. }
  992. wmi_tlv_os_mem_free(*wmi_cmd_struct_ptr);
  993. *wmi_cmd_struct_ptr = NULL;
  994. #endif
  995. return;
  996. }
  997. /**
  998. * wmitlv_free_allocated_command_tlvs() - tlv helper function
  999. * @cmd_event_id: command or event id
  1000. * @wmi_cmd_struct_ptr: wmi command structure
  1001. *
  1002. *
  1003. * free any allocated buffers for WMI Event/Command TLV processing
  1004. *
  1005. * Return: none
  1006. */
  1007. void wmitlv_free_allocated_command_tlvs(uint32_t cmd_event_id,
  1008. void **wmi_cmd_struct_ptr)
  1009. {
  1010. wmitlv_free_allocated_tlvs(1, cmd_event_id, wmi_cmd_struct_ptr);
  1011. }
  1012. /**
  1013. * wmitlv_free_allocated_event_tlvs() - tlv helper function
  1014. * @cmd_event_id: command or event id
  1015. * @wmi_cmd_struct_ptr: wmi command structure
  1016. *
  1017. *
  1018. * free any allocated buffers for WMI Event/Command TLV processing
  1019. *
  1020. * Return: none
  1021. */
  1022. void wmitlv_free_allocated_event_tlvs(uint32_t cmd_event_id,
  1023. void **wmi_cmd_struct_ptr)
  1024. {
  1025. wmitlv_free_allocated_tlvs(0, cmd_event_id, wmi_cmd_struct_ptr);
  1026. }
  1027. qdf_export_symbol(wmitlv_free_allocated_event_tlvs);
  1028. /**
  1029. * wmi_versions_are_compatible() - tlv helper function
  1030. * @vers1: host wmi version
  1031. * @vers2: target wmi version
  1032. *
  1033. *
  1034. * check if two given wmi versions are compatible
  1035. *
  1036. * Return: none
  1037. */
  1038. int
  1039. wmi_versions_are_compatible(wmi_abi_version *vers1, wmi_abi_version *vers2)
  1040. {
  1041. if ((vers1->abi_version_ns_0 != vers2->abi_version_ns_0) ||
  1042. (vers1->abi_version_ns_1 != vers2->abi_version_ns_1) ||
  1043. (vers1->abi_version_ns_2 != vers2->abi_version_ns_2) ||
  1044. (vers1->abi_version_ns_3 != vers2->abi_version_ns_3)) {
  1045. /* The namespaces are different. Incompatible. */
  1046. return 0;
  1047. }
  1048. if (vers1->abi_version_0 != vers2->abi_version_0) {
  1049. /* The major or minor versions are different. Incompatible */
  1050. return 0;
  1051. }
  1052. /* We ignore the build version */
  1053. return 1;
  1054. }
  1055. /**
  1056. * wmi_versions_can_downgrade() - tlv helper function
  1057. * @version_whitelist_table: version table
  1058. * @my_vers: host version
  1059. * @opp_vers: target version
  1060. * @out_vers: downgraded version
  1061. *
  1062. *
  1063. * check if target wmi version can be downgraded
  1064. *
  1065. * Return: 0 if success. Return < 0 if failure.
  1066. */
  1067. static int
  1068. wmi_versions_can_downgrade(int num_allowlist,
  1069. wmi_whitelist_version_info *version_whitelist_table,
  1070. wmi_abi_version *my_vers,
  1071. wmi_abi_version *opp_vers,
  1072. wmi_abi_version *out_vers)
  1073. {
  1074. uint8_t can_try_to_downgrade;
  1075. uint32_t my_major_vers = WMI_VER_GET_MAJOR(my_vers->abi_version_0);
  1076. uint32_t my_minor_vers = WMI_VER_GET_MINOR(my_vers->abi_version_0);
  1077. uint32_t opp_major_vers = WMI_VER_GET_MAJOR(opp_vers->abi_version_0);
  1078. uint32_t opp_minor_vers = WMI_VER_GET_MINOR(opp_vers->abi_version_0);
  1079. uint32_t downgraded_minor_vers;
  1080. if ((my_vers->abi_version_ns_0 != opp_vers->abi_version_ns_0) ||
  1081. (my_vers->abi_version_ns_1 != opp_vers->abi_version_ns_1) ||
  1082. (my_vers->abi_version_ns_2 != opp_vers->abi_version_ns_2) ||
  1083. (my_vers->abi_version_ns_3 != opp_vers->abi_version_ns_3)) {
  1084. /* The namespaces are different. Incompatible. */
  1085. can_try_to_downgrade = false;
  1086. } else if (my_major_vers != opp_major_vers) {
  1087. /* Major version is different. Incompatible and cannot downgrade. */
  1088. can_try_to_downgrade = false;
  1089. } else {
  1090. /* Same major version. */
  1091. if (my_minor_vers < opp_minor_vers) {
  1092. /* Opposite party is newer. Incompatible and cannot downgrade. */
  1093. can_try_to_downgrade = false;
  1094. } else if (my_minor_vers > opp_minor_vers) {
  1095. /* Opposite party is older. Check allowlist if
  1096. * we can downgrade
  1097. */
  1098. can_try_to_downgrade = true;
  1099. } else {
  1100. /* Same version */
  1101. wmi_tlv_OS_MEMCPY(out_vers, my_vers,
  1102. sizeof(wmi_abi_version));
  1103. return 1;
  1104. }
  1105. }
  1106. if (!can_try_to_downgrade) {
  1107. wmi_tlv_print_error("%s: Warning: incompatible WMI version.\n",
  1108. __func__);
  1109. wmi_tlv_OS_MEMCPY(out_vers, my_vers, sizeof(wmi_abi_version));
  1110. return 0;
  1111. }
  1112. /* Try to see we can downgrade the supported version */
  1113. downgraded_minor_vers = my_minor_vers;
  1114. while (downgraded_minor_vers > opp_minor_vers) {
  1115. uint8_t downgraded = false;
  1116. int i;
  1117. for (i = 0; i < num_allowlist; i++) {
  1118. if (version_whitelist_table[i].major != my_major_vers)
  1119. continue; /* skip */
  1120. if (version_whitelist_table[i].namespace_0 !=
  1121. my_vers->abi_version_ns_0 ||
  1122. version_whitelist_table[i].namespace_1 !=
  1123. my_vers->abi_version_ns_1 ||
  1124. version_whitelist_table[i].namespace_2 !=
  1125. my_vers->abi_version_ns_2 ||
  1126. version_whitelist_table[i].namespace_3 !=
  1127. my_vers->abi_version_ns_3) {
  1128. continue; /* skip */
  1129. }
  1130. if (version_whitelist_table[i].minor ==
  1131. downgraded_minor_vers) {
  1132. /* Found the next version that I can downgrade */
  1133. wmi_tlv_print_error
  1134. ("%s: Note: found a allowlist entry to downgrade. wh. list ver: %d,%d,0x%x 0x%x 0x%x 0x%x\n",
  1135. __func__,
  1136. version_whitelist_table[i].major,
  1137. version_whitelist_table[i].minor,
  1138. version_whitelist_table[i].namespace_0,
  1139. version_whitelist_table[i].namespace_1,
  1140. version_whitelist_table[i].namespace_2,
  1141. version_whitelist_table[i].namespace_3);
  1142. downgraded_minor_vers--;
  1143. downgraded = true;
  1144. break;
  1145. }
  1146. }
  1147. if (!downgraded) {
  1148. break; /* Done since we did not find any allowlist
  1149. * to downgrade version
  1150. */
  1151. }
  1152. }
  1153. wmi_tlv_OS_MEMCPY(out_vers, my_vers, sizeof(wmi_abi_version));
  1154. out_vers->abi_version_0 =
  1155. WMI_VER_GET_VERSION_0(my_major_vers, downgraded_minor_vers);
  1156. if (downgraded_minor_vers != opp_minor_vers) {
  1157. wmi_tlv_print_error
  1158. ("%s: Warning: incompatible WMI version and cannot downgrade.\n",
  1159. __func__);
  1160. return 0; /* Incompatible */
  1161. } else {
  1162. return 1; /* Compatible */
  1163. }
  1164. }
  1165. /**
  1166. * wmi_cmp_and_set_abi_version() - tlv helper function
  1167. * @version_whitelist_table: version table
  1168. * @my_vers: host version
  1169. * @opp_vers: target version
  1170. * @out_vers: downgraded version
  1171. *
  1172. * This routine will compare and set the WMI ABI version.
  1173. * First, compare my version with the opposite side's version.
  1174. * If incompatible, then check the allowlist to see if our side can downgrade.
  1175. * Finally, fill in the final ABI version into the output, out_vers.
  1176. * Return 0 if the output version is compatible
  1177. * Else return 1 if the output version is incompatible
  1178. *
  1179. * Return: 0 if the output version is compatible else < 0.
  1180. */
  1181. int
  1182. wmi_cmp_and_set_abi_version(int num_allowlist,
  1183. wmi_whitelist_version_info *
  1184. version_whitelist_table,
  1185. struct _wmi_abi_version *my_vers,
  1186. struct _wmi_abi_version *opp_vers,
  1187. struct _wmi_abi_version *out_vers)
  1188. {
  1189. wmi_tlv_print_verbose
  1190. ("%s: Our WMI Version: Mj=%d, Mn=%d, bd=%d, ns0=0x%x ns1:0x%x ns2:0x%x ns3:0x%x\n",
  1191. __func__, WMI_VER_GET_MAJOR(my_vers->abi_version_0),
  1192. WMI_VER_GET_MINOR(my_vers->abi_version_0), my_vers->abi_version_1,
  1193. my_vers->abi_version_ns_0, my_vers->abi_version_ns_1,
  1194. my_vers->abi_version_ns_2, my_vers->abi_version_ns_3);
  1195. wmi_tlv_print_verbose
  1196. ("%s: Opposite side WMI Version: Mj=%d, Mn=%d, bd=%d, ns0=0x%x ns1:0x%x ns2:0x%x ns3:0x%x\n",
  1197. __func__, WMI_VER_GET_MAJOR(opp_vers->abi_version_0),
  1198. WMI_VER_GET_MINOR(opp_vers->abi_version_0),
  1199. opp_vers->abi_version_1, opp_vers->abi_version_ns_0,
  1200. opp_vers->abi_version_ns_1, opp_vers->abi_version_ns_2,
  1201. opp_vers->abi_version_ns_3);
  1202. /* By default, the output version is our version. */
  1203. wmi_tlv_OS_MEMCPY(out_vers, my_vers, sizeof(wmi_abi_version));
  1204. if (!wmi_versions_are_compatible(my_vers, opp_vers)) {
  1205. /* Our host version and the given firmware version are incompatible. */
  1206. if (wmi_versions_can_downgrade(num_allowlist,
  1207. version_whitelist_table,
  1208. my_vers,
  1209. opp_vers,
  1210. out_vers)) {
  1211. /* We can downgrade our host versions to match firmware. */
  1212. wmi_tlv_print_error
  1213. ("%s: Host downgraded WMI Versions to match fw. Ret version: Mj=%d, Mn=%d, bd=%d, ns0=0x%x ns1:0x%x ns2:0x%x ns3:0x%x\n",
  1214. __func__,
  1215. WMI_VER_GET_MAJOR(out_vers->abi_version_0),
  1216. WMI_VER_GET_MINOR(out_vers->abi_version_0),
  1217. out_vers->abi_version_1,
  1218. out_vers->abi_version_ns_0,
  1219. out_vers->abi_version_ns_1,
  1220. out_vers->abi_version_ns_2,
  1221. out_vers->abi_version_ns_3);
  1222. return 0; /* Compatible */
  1223. } else {
  1224. /* Warn: We cannot downgrade our host versions to match firmware. */
  1225. wmi_tlv_print_error
  1226. ("%s: WARN: Host WMI Versions mismatch with fw. Ret version: Mj=%d, Mn=%d, bd=%d, ns0=0x%x ns1:0x%x ns2:0x%x ns3:0x%x\n",
  1227. __func__,
  1228. WMI_VER_GET_MAJOR(out_vers->abi_version_0),
  1229. WMI_VER_GET_MINOR(out_vers->abi_version_0),
  1230. out_vers->abi_version_1,
  1231. out_vers->abi_version_ns_0,
  1232. out_vers->abi_version_ns_1,
  1233. out_vers->abi_version_ns_2,
  1234. out_vers->abi_version_ns_3);
  1235. return 1; /* Incompatible */
  1236. }
  1237. } else {
  1238. /* We are compatible. Our host version is the output version */
  1239. wmi_tlv_print_verbose
  1240. ("%s: Host and FW Compatible WMI Versions. Ret version: Mj=%d, Mn=%d, bd=%d, ns0=0x%x ns1:0x%x ns2:0x%x ns3:0x%x\n",
  1241. __func__, WMI_VER_GET_MAJOR(out_vers->abi_version_0),
  1242. WMI_VER_GET_MINOR(out_vers->abi_version_0),
  1243. out_vers->abi_version_1, out_vers->abi_version_ns_0,
  1244. out_vers->abi_version_ns_1, out_vers->abi_version_ns_2,
  1245. out_vers->abi_version_ns_3);
  1246. return 0; /* Compatible */
  1247. }
  1248. }