wmi_tlv_helper.c 42 KB

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