mlxfw_fsm.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * drivers/net/ethernet/mellanox/mlxfw/mlxfw.c
  3. * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #define pr_fmt(fmt) "mlxfw: " fmt
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/delay.h>
  38. #include "mlxfw.h"
  39. #include "mlxfw_mfa2.h"
  40. #define MLXFW_FSM_STATE_WAIT_CYCLE_MS 200
  41. #define MLXFW_FSM_STATE_WAIT_TIMEOUT_MS 30000
  42. #define MLXFW_FSM_STATE_WAIT_ROUNDS \
  43. (MLXFW_FSM_STATE_WAIT_TIMEOUT_MS / MLXFW_FSM_STATE_WAIT_CYCLE_MS)
  44. #define MLXFW_FSM_MAX_COMPONENT_SIZE (10 * (1 << 20))
  45. static const char * const mlxfw_fsm_state_err_str[] = {
  46. [MLXFW_FSM_STATE_ERR_ERROR] =
  47. "general error",
  48. [MLXFW_FSM_STATE_ERR_REJECTED_DIGEST_ERR] =
  49. "component hash mismatch",
  50. [MLXFW_FSM_STATE_ERR_REJECTED_NOT_APPLICABLE] =
  51. "component not applicable",
  52. [MLXFW_FSM_STATE_ERR_REJECTED_UNKNOWN_KEY] =
  53. "unknown key",
  54. [MLXFW_FSM_STATE_ERR_REJECTED_AUTH_FAILED] =
  55. "authentication failed",
  56. [MLXFW_FSM_STATE_ERR_REJECTED_UNSIGNED] =
  57. "component was not signed",
  58. [MLXFW_FSM_STATE_ERR_REJECTED_KEY_NOT_APPLICABLE] =
  59. "key not applicable",
  60. [MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT] =
  61. "bad format",
  62. [MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET] =
  63. "pending reset",
  64. [MLXFW_FSM_STATE_ERR_MAX] =
  65. "unknown error"
  66. };
  67. static int mlxfw_fsm_state_wait(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
  68. enum mlxfw_fsm_state fsm_state)
  69. {
  70. enum mlxfw_fsm_state_err fsm_state_err;
  71. enum mlxfw_fsm_state curr_fsm_state;
  72. int times;
  73. int err;
  74. times = MLXFW_FSM_STATE_WAIT_ROUNDS;
  75. retry:
  76. err = mlxfw_dev->ops->fsm_query_state(mlxfw_dev, fwhandle,
  77. &curr_fsm_state, &fsm_state_err);
  78. if (err)
  79. return err;
  80. if (fsm_state_err != MLXFW_FSM_STATE_ERR_OK) {
  81. fsm_state_err = min_t(enum mlxfw_fsm_state_err,
  82. fsm_state_err, MLXFW_FSM_STATE_ERR_MAX);
  83. pr_err("Firmware flash failed: %s\n",
  84. mlxfw_fsm_state_err_str[fsm_state_err]);
  85. return -EINVAL;
  86. }
  87. if (curr_fsm_state != fsm_state) {
  88. if (--times == 0) {
  89. pr_err("Timeout reached on FSM state change");
  90. return -ETIMEDOUT;
  91. }
  92. msleep(MLXFW_FSM_STATE_WAIT_CYCLE_MS);
  93. goto retry;
  94. }
  95. return 0;
  96. }
  97. #define MLXFW_ALIGN_DOWN(x, align_bits) ((x) & ~((1 << (align_bits)) - 1))
  98. #define MLXFW_ALIGN_UP(x, align_bits) \
  99. MLXFW_ALIGN_DOWN((x) + ((1 << (align_bits)) - 1), (align_bits))
  100. static int mlxfw_flash_component(struct mlxfw_dev *mlxfw_dev,
  101. u32 fwhandle,
  102. struct mlxfw_mfa2_component *comp)
  103. {
  104. u16 comp_max_write_size;
  105. u8 comp_align_bits;
  106. u32 comp_max_size;
  107. u16 block_size;
  108. u8 *block_ptr;
  109. u32 offset;
  110. int err;
  111. err = mlxfw_dev->ops->component_query(mlxfw_dev, comp->index,
  112. &comp_max_size, &comp_align_bits,
  113. &comp_max_write_size);
  114. if (err)
  115. return err;
  116. comp_max_size = min_t(u32, comp_max_size, MLXFW_FSM_MAX_COMPONENT_SIZE);
  117. if (comp->data_size > comp_max_size) {
  118. pr_err("Component %d is of size %d which is bigger than limit %d\n",
  119. comp->index, comp->data_size, comp_max_size);
  120. return -EINVAL;
  121. }
  122. comp_max_write_size = MLXFW_ALIGN_DOWN(comp_max_write_size,
  123. comp_align_bits);
  124. pr_debug("Component update\n");
  125. err = mlxfw_dev->ops->fsm_component_update(mlxfw_dev, fwhandle,
  126. comp->index,
  127. comp->data_size);
  128. if (err)
  129. return err;
  130. err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
  131. MLXFW_FSM_STATE_DOWNLOAD);
  132. if (err)
  133. goto err_out;
  134. pr_debug("Component download\n");
  135. for (offset = 0;
  136. offset < MLXFW_ALIGN_UP(comp->data_size, comp_align_bits);
  137. offset += comp_max_write_size) {
  138. block_ptr = comp->data + offset;
  139. block_size = (u16) min_t(u32, comp->data_size - offset,
  140. comp_max_write_size);
  141. err = mlxfw_dev->ops->fsm_block_download(mlxfw_dev, fwhandle,
  142. block_ptr, block_size,
  143. offset);
  144. if (err)
  145. goto err_out;
  146. }
  147. pr_debug("Component verify\n");
  148. err = mlxfw_dev->ops->fsm_component_verify(mlxfw_dev, fwhandle,
  149. comp->index);
  150. if (err)
  151. goto err_out;
  152. err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
  153. if (err)
  154. goto err_out;
  155. return 0;
  156. err_out:
  157. mlxfw_dev->ops->fsm_cancel(mlxfw_dev, fwhandle);
  158. return err;
  159. }
  160. static int mlxfw_flash_components(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
  161. struct mlxfw_mfa2_file *mfa2_file)
  162. {
  163. u32 component_count;
  164. int err;
  165. int i;
  166. err = mlxfw_mfa2_file_component_count(mfa2_file, mlxfw_dev->psid,
  167. mlxfw_dev->psid_size,
  168. &component_count);
  169. if (err) {
  170. pr_err("Could not find device PSID in MFA2 file\n");
  171. return err;
  172. }
  173. for (i = 0; i < component_count; i++) {
  174. struct mlxfw_mfa2_component *comp;
  175. comp = mlxfw_mfa2_file_component_get(mfa2_file, mlxfw_dev->psid,
  176. mlxfw_dev->psid_size, i);
  177. if (IS_ERR(comp))
  178. return PTR_ERR(comp);
  179. pr_info("Flashing component type %d\n", comp->index);
  180. err = mlxfw_flash_component(mlxfw_dev, fwhandle, comp);
  181. mlxfw_mfa2_file_component_put(comp);
  182. if (err)
  183. return err;
  184. }
  185. return 0;
  186. }
  187. int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
  188. const struct firmware *firmware)
  189. {
  190. struct mlxfw_mfa2_file *mfa2_file;
  191. u32 fwhandle;
  192. int err;
  193. if (!mlxfw_mfa2_check(firmware)) {
  194. pr_err("Firmware file is not MFA2\n");
  195. return -EINVAL;
  196. }
  197. mfa2_file = mlxfw_mfa2_file_init(firmware);
  198. if (IS_ERR(mfa2_file))
  199. return PTR_ERR(mfa2_file);
  200. pr_info("Initialize firmware flash process\n");
  201. err = mlxfw_dev->ops->fsm_lock(mlxfw_dev, &fwhandle);
  202. if (err) {
  203. pr_err("Could not lock the firmware FSM\n");
  204. goto err_fsm_lock;
  205. }
  206. err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
  207. MLXFW_FSM_STATE_LOCKED);
  208. if (err)
  209. goto err_state_wait_idle_to_locked;
  210. err = mlxfw_flash_components(mlxfw_dev, fwhandle, mfa2_file);
  211. if (err)
  212. goto err_flash_components;
  213. pr_debug("Activate image\n");
  214. err = mlxfw_dev->ops->fsm_activate(mlxfw_dev, fwhandle);
  215. if (err) {
  216. pr_err("Could not activate the downloaded image\n");
  217. goto err_fsm_activate;
  218. }
  219. err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
  220. if (err)
  221. goto err_state_wait_activate_to_locked;
  222. pr_debug("Handle release\n");
  223. mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
  224. pr_info("Firmware flash done.\n");
  225. mlxfw_mfa2_file_fini(mfa2_file);
  226. return 0;
  227. err_state_wait_activate_to_locked:
  228. err_fsm_activate:
  229. err_flash_components:
  230. err_state_wait_idle_to_locked:
  231. mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
  232. err_fsm_lock:
  233. mlxfw_mfa2_file_fini(mfa2_file);
  234. return err;
  235. }
  236. EXPORT_SYMBOL(mlxfw_firmware_flash);
  237. MODULE_LICENSE("Dual BSD/GPL");
  238. MODULE_AUTHOR("Yotam Gigi <yotamg@mellanox.com>");
  239. MODULE_DESCRIPTION("Mellanox firmware flash lib");