fm10k_tlv.c 24 KB

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