wmi_tlv_helper.c 41 KB

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