wmi_tlv_helper.c 43 KB

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