fm10k_tlv.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include "fm10k_tlv.h"
  4. /**
  5. * fm10k_tlv_msg_init - Initialize message block for TLV data storage
  6. * @msg: Pointer to message block
  7. * @msg_id: Message ID indicating message type
  8. *
  9. * This function return success if provided with a valid message pointer
  10. **/
  11. s32 fm10k_tlv_msg_init(u32 *msg, u16 msg_id)
  12. {
  13. /* verify pointer is not NULL */
  14. if (!msg)
  15. return FM10K_ERR_PARAM;
  16. *msg = (FM10K_TLV_FLAGS_MSG << FM10K_TLV_FLAGS_SHIFT) | msg_id;
  17. return 0;
  18. }
  19. /**
  20. * fm10k_tlv_attr_put_null_string - Place null terminated string on message
  21. * @msg: Pointer to message block
  22. * @attr_id: Attribute ID
  23. * @string: Pointer to string to be stored in attribute
  24. *
  25. * This function will reorder a string to be CPU endian and store it in
  26. * the attribute buffer. It will return success if provided with a valid
  27. * pointers.
  28. **/
  29. static s32 fm10k_tlv_attr_put_null_string(u32 *msg, u16 attr_id,
  30. const unsigned char *string)
  31. {
  32. u32 attr_data = 0, len = 0;
  33. u32 *attr;
  34. /* verify pointers are not NULL */
  35. if (!string || !msg)
  36. return FM10K_ERR_PARAM;
  37. attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
  38. /* copy string into local variable and then write to msg */
  39. do {
  40. /* write data to message */
  41. if (len && !(len % 4)) {
  42. attr[len / 4] = attr_data;
  43. attr_data = 0;
  44. }
  45. /* record character to offset location */
  46. attr_data |= (u32)(*string) << (8 * (len % 4));
  47. len++;
  48. /* test for NULL and then increment */
  49. } while (*(string++));
  50. /* write last piece of data to message */
  51. attr[(len + 3) / 4] = attr_data;
  52. /* record attribute header, update message length */
  53. len <<= FM10K_TLV_LEN_SHIFT;
  54. attr[0] = len | attr_id;
  55. /* add header length to length */
  56. len += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
  57. *msg += FM10K_TLV_LEN_ALIGN(len);
  58. return 0;
  59. }
  60. /**
  61. * fm10k_tlv_attr_get_null_string - Get null terminated string from attribute
  62. * @attr: Pointer to attribute
  63. * @string: Pointer to location of destination string
  64. *
  65. * This function pulls the string back out of the attribute and will place
  66. * it in the array pointed by by string. It will return success if provided
  67. * with a valid pointers.
  68. **/
  69. static s32 fm10k_tlv_attr_get_null_string(u32 *attr, unsigned char *string)
  70. {
  71. u32 len;
  72. /* verify pointers are not NULL */
  73. if (!string || !attr)
  74. return FM10K_ERR_PARAM;
  75. len = *attr >> FM10K_TLV_LEN_SHIFT;
  76. attr++;
  77. while (len--)
  78. string[len] = (u8)(attr[len / 4] >> (8 * (len % 4)));
  79. return 0;
  80. }
  81. /**
  82. * fm10k_tlv_attr_put_mac_vlan - Store MAC/VLAN attribute in message
  83. * @msg: Pointer to message block
  84. * @attr_id: Attribute ID
  85. * @mac_addr: MAC address to be stored
  86. * @vlan: VLAN to be stored
  87. *
  88. * This function will reorder a MAC address to be CPU endian and store it
  89. * in the attribute buffer. It will return success if provided with a
  90. * valid pointers.
  91. **/
  92. s32 fm10k_tlv_attr_put_mac_vlan(u32 *msg, u16 attr_id,
  93. const u8 *mac_addr, u16 vlan)
  94. {
  95. u32 len = ETH_ALEN << FM10K_TLV_LEN_SHIFT;
  96. u32 *attr;
  97. /* verify pointers are not NULL */
  98. if (!msg || !mac_addr)
  99. return FM10K_ERR_PARAM;
  100. attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
  101. /* record attribute header, update message length */
  102. attr[0] = len | attr_id;
  103. /* copy value into local variable and then write to msg */
  104. attr[1] = le32_to_cpu(*(const __le32 *)&mac_addr[0]);
  105. attr[2] = le16_to_cpu(*(const __le16 *)&mac_addr[4]);
  106. attr[2] |= (u32)vlan << 16;
  107. /* add header length to length */
  108. len += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
  109. *msg += FM10K_TLV_LEN_ALIGN(len);
  110. return 0;
  111. }
  112. /**
  113. * fm10k_tlv_attr_get_mac_vlan - Get MAC/VLAN stored in attribute
  114. * @attr: Pointer to attribute
  115. * @mac_addr: location of buffer to store MAC address
  116. * @vlan: location of buffer to store VLAN
  117. *
  118. * This function pulls the MAC address back out of the attribute and will
  119. * place it in the array pointed by by mac_addr. It will return success
  120. * if provided with a valid pointers.
  121. **/
  122. s32 fm10k_tlv_attr_get_mac_vlan(u32 *attr, u8 *mac_addr, u16 *vlan)
  123. {
  124. /* verify pointers are not NULL */
  125. if (!mac_addr || !attr)
  126. return FM10K_ERR_PARAM;
  127. *(__le32 *)&mac_addr[0] = cpu_to_le32(attr[1]);
  128. *(__le16 *)&mac_addr[4] = cpu_to_le16((u16)(attr[2]));
  129. *vlan = (u16)(attr[2] >> 16);
  130. return 0;
  131. }
  132. /**
  133. * fm10k_tlv_attr_put_bool - Add header indicating value "true"
  134. * @msg: Pointer to message block
  135. * @attr_id: Attribute ID
  136. *
  137. * This function will simply add an attribute header, the fact
  138. * that the header is here means the attribute value is true, else
  139. * it is false. The function will return success if provided with a
  140. * valid pointers.
  141. **/
  142. s32 fm10k_tlv_attr_put_bool(u32 *msg, u16 attr_id)
  143. {
  144. /* verify pointers are not NULL */
  145. if (!msg)
  146. return FM10K_ERR_PARAM;
  147. /* record attribute header */
  148. msg[FM10K_TLV_DWORD_LEN(*msg)] = attr_id;
  149. /* add header length to length */
  150. *msg += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
  151. return 0;
  152. }
  153. /**
  154. * fm10k_tlv_attr_put_value - Store integer value attribute in message
  155. * @msg: Pointer to message block
  156. * @attr_id: Attribute ID
  157. * @value: Value to be written
  158. * @len: Size of value
  159. *
  160. * This function will place an integer value of up to 8 bytes in size
  161. * in a message attribute. The function will return success provided
  162. * that msg is a valid pointer, and len is 1, 2, 4, or 8.
  163. **/
  164. s32 fm10k_tlv_attr_put_value(u32 *msg, u16 attr_id, s64 value, u32 len)
  165. {
  166. u32 *attr;
  167. /* verify non-null msg and len is 1, 2, 4, or 8 */
  168. if (!msg || !len || len > 8 || (len & (len - 1)))
  169. return FM10K_ERR_PARAM;
  170. attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
  171. if (len < 4) {
  172. attr[1] = (u32)value & (BIT(8 * len) - 1);
  173. } else {
  174. attr[1] = (u32)value;
  175. if (len > 4)
  176. attr[2] = (u32)(value >> 32);
  177. }
  178. /* record attribute header, update message length */
  179. len <<= FM10K_TLV_LEN_SHIFT;
  180. attr[0] = len | attr_id;
  181. /* add header length to length */
  182. len += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
  183. *msg += FM10K_TLV_LEN_ALIGN(len);
  184. return 0;
  185. }
  186. /**
  187. * fm10k_tlv_attr_get_value - Get integer value stored in attribute
  188. * @attr: Pointer to attribute
  189. * @value: Pointer to destination buffer
  190. * @len: Size of value
  191. *
  192. * This function will place an integer value of up to 8 bytes in size
  193. * in the offset pointed to by value. The function will return success
  194. * provided that pointers are valid and the len value matches the
  195. * attribute length.
  196. **/
  197. s32 fm10k_tlv_attr_get_value(u32 *attr, void *value, u32 len)
  198. {
  199. /* verify pointers are not NULL */
  200. if (!attr || !value)
  201. return FM10K_ERR_PARAM;
  202. if ((*attr >> FM10K_TLV_LEN_SHIFT) != len)
  203. return FM10K_ERR_PARAM;
  204. if (len == 8)
  205. *(u64 *)value = ((u64)attr[2] << 32) | attr[1];
  206. else if (len == 4)
  207. *(u32 *)value = attr[1];
  208. else if (len == 2)
  209. *(u16 *)value = (u16)attr[1];
  210. else
  211. *(u8 *)value = (u8)attr[1];
  212. return 0;
  213. }
  214. /**
  215. * fm10k_tlv_attr_put_le_struct - Store little endian structure in message
  216. * @msg: Pointer to message block
  217. * @attr_id: Attribute ID
  218. * @le_struct: Pointer to structure to be written
  219. * @len: Size of le_struct
  220. *
  221. * This function will place a little endian structure value in a message
  222. * attribute. The function will return success provided that all pointers
  223. * are valid and length is a non-zero multiple of 4.
  224. **/
  225. s32 fm10k_tlv_attr_put_le_struct(u32 *msg, u16 attr_id,
  226. const void *le_struct, u32 len)
  227. {
  228. const __le32 *le32_ptr = (const __le32 *)le_struct;
  229. u32 *attr;
  230. u32 i;
  231. /* verify non-null msg and len is in 32 bit words */
  232. if (!msg || !len || (len % 4))
  233. return FM10K_ERR_PARAM;
  234. attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
  235. /* copy le32 structure into host byte order at 32b boundaries */
  236. for (i = 0; i < (len / 4); i++)
  237. attr[i + 1] = le32_to_cpu(le32_ptr[i]);
  238. /* record attribute header, update message length */
  239. len <<= FM10K_TLV_LEN_SHIFT;
  240. attr[0] = len | attr_id;
  241. /* add header length to length */
  242. len += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
  243. *msg += FM10K_TLV_LEN_ALIGN(len);
  244. return 0;
  245. }
  246. /**
  247. * fm10k_tlv_attr_get_le_struct - Get little endian struct form attribute
  248. * @attr: Pointer to attribute
  249. * @le_struct: Pointer to structure to be written
  250. * @len: Size of structure
  251. *
  252. * This function will place a little endian structure in the buffer
  253. * pointed to by le_struct. The function will return success
  254. * provided that pointers are valid and the len value matches the
  255. * attribute length.
  256. **/
  257. s32 fm10k_tlv_attr_get_le_struct(u32 *attr, void *le_struct, u32 len)
  258. {
  259. __le32 *le32_ptr = (__le32 *)le_struct;
  260. u32 i;
  261. /* verify pointers are not NULL */
  262. if (!le_struct || !attr)
  263. return FM10K_ERR_PARAM;
  264. if ((*attr >> FM10K_TLV_LEN_SHIFT) != len)
  265. return FM10K_ERR_PARAM;
  266. attr++;
  267. for (i = 0; len; i++, len -= 4)
  268. le32_ptr[i] = cpu_to_le32(attr[i]);
  269. return 0;
  270. }
  271. /**
  272. * fm10k_tlv_attr_nest_start - Start a set of nested attributes
  273. * @msg: Pointer to message block
  274. * @attr_id: Attribute ID
  275. *
  276. * This function will mark off a new nested region for encapsulating
  277. * a given set of attributes. The idea is if you wish to place a secondary
  278. * structure within the message this mechanism allows for that. The
  279. * function will return NULL on failure, and a pointer to the start
  280. * of the nested attributes on success.
  281. **/
  282. static u32 *fm10k_tlv_attr_nest_start(u32 *msg, u16 attr_id)
  283. {
  284. u32 *attr;
  285. /* verify pointer is not NULL */
  286. if (!msg)
  287. return NULL;
  288. attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
  289. attr[0] = attr_id;
  290. /* return pointer to nest header */
  291. return attr;
  292. }
  293. /**
  294. * fm10k_tlv_attr_nest_stop - Stop a set of nested attributes
  295. * @msg: Pointer to message block
  296. *
  297. * This function closes off an existing set of nested attributes. The
  298. * message pointer should be pointing to the parent of the nest. So in
  299. * the case of a nest within the nest this would be the outer nest pointer.
  300. * This function will return success provided all pointers are valid.
  301. **/
  302. static s32 fm10k_tlv_attr_nest_stop(u32 *msg)
  303. {
  304. u32 *attr;
  305. u32 len;
  306. /* verify pointer is not NULL */
  307. if (!msg)
  308. return FM10K_ERR_PARAM;
  309. /* locate the nested header and retrieve its length */
  310. attr = &msg[FM10K_TLV_DWORD_LEN(*msg)];
  311. len = (attr[0] >> FM10K_TLV_LEN_SHIFT) << FM10K_TLV_LEN_SHIFT;
  312. /* only include nest if data was added to it */
  313. if (len) {
  314. len += FM10K_TLV_HDR_LEN << FM10K_TLV_LEN_SHIFT;
  315. *msg += len;
  316. }
  317. return 0;
  318. }
  319. /**
  320. * fm10k_tlv_attr_validate - Validate attribute metadata
  321. * @attr: Pointer to attribute
  322. * @tlv_attr: Type and length info for attribute
  323. *
  324. * This function does some basic validation of the input TLV. It
  325. * verifies the length, and in the case of null terminated strings
  326. * it verifies that the last byte is null. The function will
  327. * return FM10K_ERR_PARAM if any attribute is malformed, otherwise
  328. * it returns 0.
  329. **/
  330. static s32 fm10k_tlv_attr_validate(u32 *attr,
  331. const struct fm10k_tlv_attr *tlv_attr)
  332. {
  333. u32 attr_id = *attr & FM10K_TLV_ID_MASK;
  334. u16 len = *attr >> FM10K_TLV_LEN_SHIFT;
  335. /* verify this is an attribute and not a message */
  336. if (*attr & (FM10K_TLV_FLAGS_MSG << FM10K_TLV_FLAGS_SHIFT))
  337. return FM10K_ERR_PARAM;
  338. /* search through the list of attributes to find a matching ID */
  339. while (tlv_attr->id < attr_id)
  340. tlv_attr++;
  341. /* if didn't find a match then we should exit */
  342. if (tlv_attr->id != attr_id)
  343. return FM10K_NOT_IMPLEMENTED;
  344. /* move to start of attribute data */
  345. attr++;
  346. switch (tlv_attr->type) {
  347. case FM10K_TLV_NULL_STRING:
  348. if (!len ||
  349. (attr[(len - 1) / 4] & (0xFF << (8 * ((len - 1) % 4)))))
  350. return FM10K_ERR_PARAM;
  351. if (len > tlv_attr->len)
  352. return FM10K_ERR_PARAM;
  353. break;
  354. case FM10K_TLV_MAC_ADDR:
  355. if (len != ETH_ALEN)
  356. return FM10K_ERR_PARAM;
  357. break;
  358. case FM10K_TLV_BOOL:
  359. if (len)
  360. return FM10K_ERR_PARAM;
  361. break;
  362. case FM10K_TLV_UNSIGNED:
  363. case FM10K_TLV_SIGNED:
  364. if (len != tlv_attr->len)
  365. return FM10K_ERR_PARAM;
  366. break;
  367. case FM10K_TLV_LE_STRUCT:
  368. /* struct must be 4 byte aligned */
  369. if ((len % 4) || len != tlv_attr->len)
  370. return FM10K_ERR_PARAM;
  371. break;
  372. case FM10K_TLV_NESTED:
  373. /* nested attributes must be 4 byte aligned */
  374. if (len % 4)
  375. return FM10K_ERR_PARAM;
  376. break;
  377. default:
  378. /* attribute id is mapped to bad value */
  379. return FM10K_ERR_PARAM;
  380. }
  381. return 0;
  382. }
  383. /**
  384. * fm10k_tlv_attr_parse - Parses stream of attribute data
  385. * @attr: Pointer to attribute list
  386. * @results: Pointer array to store pointers to attributes
  387. * @tlv_attr: Type and length info for attributes
  388. *
  389. * This function validates a stream of attributes and parses them
  390. * up into an array of pointers stored in results. The function will
  391. * return FM10K_ERR_PARAM on any input or message error,
  392. * FM10K_NOT_IMPLEMENTED for any attribute that is outside of the array
  393. * and 0 on success. Any attributes not found in tlv_attr will be silently
  394. * ignored.
  395. **/
  396. static s32 fm10k_tlv_attr_parse(u32 *attr, u32 **results,
  397. const struct fm10k_tlv_attr *tlv_attr)
  398. {
  399. u32 i, attr_id, offset = 0;
  400. s32 err = 0;
  401. u16 len;
  402. /* verify pointers are not NULL */
  403. if (!attr || !results)
  404. return FM10K_ERR_PARAM;
  405. /* initialize results to NULL */
  406. for (i = 0; i < FM10K_TLV_RESULTS_MAX; i++)
  407. results[i] = NULL;
  408. /* pull length from the message header */
  409. len = *attr >> FM10K_TLV_LEN_SHIFT;
  410. /* no attributes to parse if there is no length */
  411. if (!len)
  412. return 0;
  413. /* no attributes to parse, just raw data, message becomes attribute */
  414. if (!tlv_attr) {
  415. results[0] = attr;
  416. return 0;
  417. }
  418. /* move to start of attribute data */
  419. attr++;
  420. /* run through list parsing all attributes */
  421. while (offset < len) {
  422. attr_id = *attr & FM10K_TLV_ID_MASK;
  423. if (attr_id >= FM10K_TLV_RESULTS_MAX)
  424. return FM10K_NOT_IMPLEMENTED;
  425. err = fm10k_tlv_attr_validate(attr, tlv_attr);
  426. if (err == FM10K_NOT_IMPLEMENTED)
  427. ; /* silently ignore non-implemented attributes */
  428. else if (err)
  429. return err;
  430. else
  431. results[attr_id] = attr;
  432. /* update offset */
  433. offset += FM10K_TLV_DWORD_LEN(*attr) * 4;
  434. /* move to next attribute */
  435. attr = &attr[FM10K_TLV_DWORD_LEN(*attr)];
  436. }
  437. /* we should find ourselves at the end of the list */
  438. if (offset != len)
  439. return FM10K_ERR_PARAM;
  440. return 0;
  441. }
  442. /**
  443. * fm10k_tlv_msg_parse - Parses message header and calls function handler
  444. * @hw: Pointer to hardware structure
  445. * @msg: Pointer to message
  446. * @mbx: Pointer to mailbox information structure
  447. * @data: Pointer to message handler data structure
  448. *
  449. * This function should be the first function called upon receiving a
  450. * message. The handler will identify the message type and call the correct
  451. * handler for the given message. It will return the value from the function
  452. * call on a recognized message type, otherwise it will return
  453. * FM10K_NOT_IMPLEMENTED on an unrecognized type.
  454. **/
  455. s32 fm10k_tlv_msg_parse(struct fm10k_hw *hw, u32 *msg,
  456. struct fm10k_mbx_info *mbx,
  457. const struct fm10k_msg_data *data)
  458. {
  459. u32 *results[FM10K_TLV_RESULTS_MAX];
  460. u32 msg_id;
  461. s32 err;
  462. /* verify pointer is not NULL */
  463. if (!msg || !data)
  464. return FM10K_ERR_PARAM;
  465. /* verify this is a message and not an attribute */
  466. if (!(*msg & (FM10K_TLV_FLAGS_MSG << FM10K_TLV_FLAGS_SHIFT)))
  467. return FM10K_ERR_PARAM;
  468. /* grab message ID */
  469. msg_id = *msg & FM10K_TLV_ID_MASK;
  470. while (data->id < msg_id)
  471. data++;
  472. /* if we didn't find it then pass it up as an error */
  473. if (data->id != msg_id) {
  474. while (data->id != FM10K_TLV_ERROR)
  475. data++;
  476. }
  477. /* parse the attributes into the results list */
  478. err = fm10k_tlv_attr_parse(msg, results, data->attr);
  479. if (err < 0)
  480. return err;
  481. return data->func(hw, results, mbx);
  482. }
  483. /**
  484. * fm10k_tlv_msg_error - Default handler for unrecognized TLV message IDs
  485. * @hw: Pointer to hardware structure
  486. * @results: Pointer array to message, results[0] is pointer to message
  487. * @mbx: Unused mailbox pointer
  488. *
  489. * This function is a default handler for unrecognized messages. At a
  490. * a minimum it just indicates that the message requested was
  491. * unimplemented.
  492. **/
  493. s32 fm10k_tlv_msg_error(struct fm10k_hw *hw, u32 **results,
  494. struct fm10k_mbx_info *mbx)
  495. {
  496. return FM10K_NOT_IMPLEMENTED;
  497. }
  498. static const unsigned char test_str[] = "fm10k";
  499. static const unsigned char test_mac[ETH_ALEN] = { 0x12, 0x34, 0x56,
  500. 0x78, 0x9a, 0xbc };
  501. static const u16 test_vlan = 0x0FED;
  502. static const u64 test_u64 = 0xfedcba9876543210ull;
  503. static const u32 test_u32 = 0x87654321;
  504. static const u16 test_u16 = 0x8765;
  505. static const u8 test_u8 = 0x87;
  506. static const s64 test_s64 = -0x123456789abcdef0ll;
  507. static const s32 test_s32 = -0x1235678;
  508. static const s16 test_s16 = -0x1234;
  509. static const s8 test_s8 = -0x12;
  510. static const __le32 test_le[2] = { cpu_to_le32(0x12345678),
  511. cpu_to_le32(0x9abcdef0)};
  512. /* The message below is meant to be used as a test message to demonstrate
  513. * how to use the TLV interface and to test the types. Normally this code
  514. * be compiled out by stripping the code wrapped in FM10K_TLV_TEST_MSG
  515. */
  516. const struct fm10k_tlv_attr fm10k_tlv_msg_test_attr[] = {
  517. FM10K_TLV_ATTR_NULL_STRING(FM10K_TEST_MSG_STRING, 80),
  518. FM10K_TLV_ATTR_MAC_ADDR(FM10K_TEST_MSG_MAC_ADDR),
  519. FM10K_TLV_ATTR_U8(FM10K_TEST_MSG_U8),
  520. FM10K_TLV_ATTR_U16(FM10K_TEST_MSG_U16),
  521. FM10K_TLV_ATTR_U32(FM10K_TEST_MSG_U32),
  522. FM10K_TLV_ATTR_U64(FM10K_TEST_MSG_U64),
  523. FM10K_TLV_ATTR_S8(FM10K_TEST_MSG_S8),
  524. FM10K_TLV_ATTR_S16(FM10K_TEST_MSG_S16),
  525. FM10K_TLV_ATTR_S32(FM10K_TEST_MSG_S32),
  526. FM10K_TLV_ATTR_S64(FM10K_TEST_MSG_S64),
  527. FM10K_TLV_ATTR_LE_STRUCT(FM10K_TEST_MSG_LE_STRUCT, 8),
  528. FM10K_TLV_ATTR_NESTED(FM10K_TEST_MSG_NESTED),
  529. FM10K_TLV_ATTR_S32(FM10K_TEST_MSG_RESULT),
  530. FM10K_TLV_ATTR_LAST
  531. };
  532. /**
  533. * fm10k_tlv_msg_test_generate_data - Stuff message with data
  534. * @msg: Pointer to message
  535. * @attr_flags: List of flags indicating what attributes to add
  536. *
  537. * This function is meant to load a message buffer with attribute data
  538. **/
  539. static void fm10k_tlv_msg_test_generate_data(u32 *msg, u32 attr_flags)
  540. {
  541. if (attr_flags & BIT(FM10K_TEST_MSG_STRING))
  542. fm10k_tlv_attr_put_null_string(msg, FM10K_TEST_MSG_STRING,
  543. test_str);
  544. if (attr_flags & BIT(FM10K_TEST_MSG_MAC_ADDR))
  545. fm10k_tlv_attr_put_mac_vlan(msg, FM10K_TEST_MSG_MAC_ADDR,
  546. test_mac, test_vlan);
  547. if (attr_flags & BIT(FM10K_TEST_MSG_U8))
  548. fm10k_tlv_attr_put_u8(msg, FM10K_TEST_MSG_U8, test_u8);
  549. if (attr_flags & BIT(FM10K_TEST_MSG_U16))
  550. fm10k_tlv_attr_put_u16(msg, FM10K_TEST_MSG_U16, test_u16);
  551. if (attr_flags & BIT(FM10K_TEST_MSG_U32))
  552. fm10k_tlv_attr_put_u32(msg, FM10K_TEST_MSG_U32, test_u32);
  553. if (attr_flags & BIT(FM10K_TEST_MSG_U64))
  554. fm10k_tlv_attr_put_u64(msg, FM10K_TEST_MSG_U64, test_u64);
  555. if (attr_flags & BIT(FM10K_TEST_MSG_S8))
  556. fm10k_tlv_attr_put_s8(msg, FM10K_TEST_MSG_S8, test_s8);
  557. if (attr_flags & BIT(FM10K_TEST_MSG_S16))
  558. fm10k_tlv_attr_put_s16(msg, FM10K_TEST_MSG_S16, test_s16);
  559. if (attr_flags & BIT(FM10K_TEST_MSG_S32))
  560. fm10k_tlv_attr_put_s32(msg, FM10K_TEST_MSG_S32, test_s32);
  561. if (attr_flags & BIT(FM10K_TEST_MSG_S64))
  562. fm10k_tlv_attr_put_s64(msg, FM10K_TEST_MSG_S64, test_s64);
  563. if (attr_flags & BIT(FM10K_TEST_MSG_LE_STRUCT))
  564. fm10k_tlv_attr_put_le_struct(msg, FM10K_TEST_MSG_LE_STRUCT,
  565. test_le, 8);
  566. }
  567. /**
  568. * fm10k_tlv_msg_test_create - Create a test message testing all attributes
  569. * @msg: Pointer to message
  570. * @attr_flags: List of flags indicating what attributes to add
  571. *
  572. * This function is meant to load a message buffer with all attribute types
  573. * including a nested attribute.
  574. **/
  575. void fm10k_tlv_msg_test_create(u32 *msg, u32 attr_flags)
  576. {
  577. u32 *nest = NULL;
  578. fm10k_tlv_msg_init(msg, FM10K_TLV_MSG_ID_TEST);
  579. fm10k_tlv_msg_test_generate_data(msg, attr_flags);
  580. /* check for nested attributes */
  581. attr_flags >>= FM10K_TEST_MSG_NESTED;
  582. if (attr_flags) {
  583. nest = fm10k_tlv_attr_nest_start(msg, FM10K_TEST_MSG_NESTED);
  584. fm10k_tlv_msg_test_generate_data(nest, attr_flags);
  585. fm10k_tlv_attr_nest_stop(msg);
  586. }
  587. }
  588. /**
  589. * fm10k_tlv_msg_test - Validate all results on test message receive
  590. * @hw: Pointer to hardware structure
  591. * @results: Pointer array to attributes in the message
  592. * @mbx: Pointer to mailbox information structure
  593. *
  594. * This function does a check to verify all attributes match what the test
  595. * message placed in the message buffer. It is the default handler
  596. * for TLV test messages.
  597. **/
  598. s32 fm10k_tlv_msg_test(struct fm10k_hw *hw, u32 **results,
  599. struct fm10k_mbx_info *mbx)
  600. {
  601. u32 *nest_results[FM10K_TLV_RESULTS_MAX];
  602. unsigned char result_str[80];
  603. unsigned char result_mac[ETH_ALEN];
  604. s32 err = 0;
  605. __le32 result_le[2];
  606. u16 result_vlan;
  607. u64 result_u64;
  608. u32 result_u32;
  609. u16 result_u16;
  610. u8 result_u8;
  611. s64 result_s64;
  612. s32 result_s32;
  613. s16 result_s16;
  614. s8 result_s8;
  615. u32 reply[3];
  616. /* retrieve results of a previous test */
  617. if (!!results[FM10K_TEST_MSG_RESULT])
  618. return fm10k_tlv_attr_get_s32(results[FM10K_TEST_MSG_RESULT],
  619. &mbx->test_result);
  620. parse_nested:
  621. if (!!results[FM10K_TEST_MSG_STRING]) {
  622. err = fm10k_tlv_attr_get_null_string(
  623. results[FM10K_TEST_MSG_STRING],
  624. result_str);
  625. if (!err && memcmp(test_str, result_str, sizeof(test_str)))
  626. err = FM10K_ERR_INVALID_VALUE;
  627. if (err)
  628. goto report_result;
  629. }
  630. if (!!results[FM10K_TEST_MSG_MAC_ADDR]) {
  631. err = fm10k_tlv_attr_get_mac_vlan(
  632. results[FM10K_TEST_MSG_MAC_ADDR],
  633. result_mac, &result_vlan);
  634. if (!err && !ether_addr_equal(test_mac, result_mac))
  635. err = FM10K_ERR_INVALID_VALUE;
  636. if (!err && test_vlan != result_vlan)
  637. err = FM10K_ERR_INVALID_VALUE;
  638. if (err)
  639. goto report_result;
  640. }
  641. if (!!results[FM10K_TEST_MSG_U8]) {
  642. err = fm10k_tlv_attr_get_u8(results[FM10K_TEST_MSG_U8],
  643. &result_u8);
  644. if (!err && test_u8 != result_u8)
  645. err = FM10K_ERR_INVALID_VALUE;
  646. if (err)
  647. goto report_result;
  648. }
  649. if (!!results[FM10K_TEST_MSG_U16]) {
  650. err = fm10k_tlv_attr_get_u16(results[FM10K_TEST_MSG_U16],
  651. &result_u16);
  652. if (!err && test_u16 != result_u16)
  653. err = FM10K_ERR_INVALID_VALUE;
  654. if (err)
  655. goto report_result;
  656. }
  657. if (!!results[FM10K_TEST_MSG_U32]) {
  658. err = fm10k_tlv_attr_get_u32(results[FM10K_TEST_MSG_U32],
  659. &result_u32);
  660. if (!err && test_u32 != result_u32)
  661. err = FM10K_ERR_INVALID_VALUE;
  662. if (err)
  663. goto report_result;
  664. }
  665. if (!!results[FM10K_TEST_MSG_U64]) {
  666. err = fm10k_tlv_attr_get_u64(results[FM10K_TEST_MSG_U64],
  667. &result_u64);
  668. if (!err && test_u64 != result_u64)
  669. err = FM10K_ERR_INVALID_VALUE;
  670. if (err)
  671. goto report_result;
  672. }
  673. if (!!results[FM10K_TEST_MSG_S8]) {
  674. err = fm10k_tlv_attr_get_s8(results[FM10K_TEST_MSG_S8],
  675. &result_s8);
  676. if (!err && test_s8 != result_s8)
  677. err = FM10K_ERR_INVALID_VALUE;
  678. if (err)
  679. goto report_result;
  680. }
  681. if (!!results[FM10K_TEST_MSG_S16]) {
  682. err = fm10k_tlv_attr_get_s16(results[FM10K_TEST_MSG_S16],
  683. &result_s16);
  684. if (!err && test_s16 != result_s16)
  685. err = FM10K_ERR_INVALID_VALUE;
  686. if (err)
  687. goto report_result;
  688. }
  689. if (!!results[FM10K_TEST_MSG_S32]) {
  690. err = fm10k_tlv_attr_get_s32(results[FM10K_TEST_MSG_S32],
  691. &result_s32);
  692. if (!err && test_s32 != result_s32)
  693. err = FM10K_ERR_INVALID_VALUE;
  694. if (err)
  695. goto report_result;
  696. }
  697. if (!!results[FM10K_TEST_MSG_S64]) {
  698. err = fm10k_tlv_attr_get_s64(results[FM10K_TEST_MSG_S64],
  699. &result_s64);
  700. if (!err && test_s64 != result_s64)
  701. err = FM10K_ERR_INVALID_VALUE;
  702. if (err)
  703. goto report_result;
  704. }
  705. if (!!results[FM10K_TEST_MSG_LE_STRUCT]) {
  706. err = fm10k_tlv_attr_get_le_struct(
  707. results[FM10K_TEST_MSG_LE_STRUCT],
  708. result_le,
  709. sizeof(result_le));
  710. if (!err && memcmp(test_le, result_le, sizeof(test_le)))
  711. err = FM10K_ERR_INVALID_VALUE;
  712. if (err)
  713. goto report_result;
  714. }
  715. if (!!results[FM10K_TEST_MSG_NESTED]) {
  716. /* clear any pointers */
  717. memset(nest_results, 0, sizeof(nest_results));
  718. /* parse the nested attributes into the nest results list */
  719. err = fm10k_tlv_attr_parse(results[FM10K_TEST_MSG_NESTED],
  720. nest_results,
  721. fm10k_tlv_msg_test_attr);
  722. if (err)
  723. goto report_result;
  724. /* loop back through to the start */
  725. results = nest_results;
  726. goto parse_nested;
  727. }
  728. report_result:
  729. /* generate reply with test result */
  730. fm10k_tlv_msg_init(reply, FM10K_TLV_MSG_ID_TEST);
  731. fm10k_tlv_attr_put_s32(reply, FM10K_TEST_MSG_RESULT, err);
  732. /* load onto outgoing mailbox */
  733. return mbx->ops.enqueue_tx(hw, mbx, reply);
  734. }