mlxfw_fsm.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. pr_err("Firmware flash failed: %s\n",
  82. mlxfw_fsm_state_err_str[fsm_state_err]);
  83. return -EINVAL;
  84. }
  85. if (curr_fsm_state != fsm_state) {
  86. if (--times == 0) {
  87. pr_err("Timeout reached on FSM state change");
  88. return -ETIMEDOUT;
  89. }
  90. msleep(MLXFW_FSM_STATE_WAIT_CYCLE_MS);
  91. goto retry;
  92. }
  93. return 0;
  94. }
  95. #define MLXFW_ALIGN_DOWN(x, align_bits) ((x) & ~((1 << (align_bits)) - 1))
  96. #define MLXFW_ALIGN_UP(x, align_bits) \
  97. MLXFW_ALIGN_DOWN((x) + ((1 << (align_bits)) - 1), (align_bits))
  98. static int mlxfw_flash_component(struct mlxfw_dev *mlxfw_dev,
  99. u32 fwhandle,
  100. struct mlxfw_mfa2_component *comp)
  101. {
  102. u16 comp_max_write_size;
  103. u8 comp_align_bits;
  104. u32 comp_max_size;
  105. u16 block_size;
  106. u8 *block_ptr;
  107. u32 offset;
  108. int err;
  109. err = mlxfw_dev->ops->component_query(mlxfw_dev, comp->index,
  110. &comp_max_size, &comp_align_bits,
  111. &comp_max_write_size);
  112. if (err)
  113. return err;
  114. comp_max_size = min_t(u32, comp_max_size, MLXFW_FSM_MAX_COMPONENT_SIZE);
  115. if (comp->data_size > comp_max_size) {
  116. pr_err("Component %d is of size %d which is bigger than limit %d\n",
  117. comp->index, comp->data_size, comp_max_size);
  118. return -EINVAL;
  119. }
  120. comp_max_write_size = MLXFW_ALIGN_DOWN(comp_max_write_size,
  121. comp_align_bits);
  122. pr_debug("Component update\n");
  123. err = mlxfw_dev->ops->fsm_component_update(mlxfw_dev, fwhandle,
  124. comp->index,
  125. comp->data_size);
  126. if (err)
  127. return err;
  128. err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
  129. MLXFW_FSM_STATE_DOWNLOAD);
  130. if (err)
  131. goto err_out;
  132. pr_debug("Component download\n");
  133. for (offset = 0;
  134. offset < MLXFW_ALIGN_UP(comp->data_size, comp_align_bits);
  135. offset += comp_max_write_size) {
  136. block_ptr = comp->data + offset;
  137. block_size = (u16) min_t(u32, comp->data_size - offset,
  138. comp_max_write_size);
  139. err = mlxfw_dev->ops->fsm_block_download(mlxfw_dev, fwhandle,
  140. block_ptr, block_size,
  141. offset);
  142. if (err)
  143. goto err_out;
  144. }
  145. pr_debug("Component verify\n");
  146. err = mlxfw_dev->ops->fsm_component_verify(mlxfw_dev, fwhandle,
  147. comp->index);
  148. if (err)
  149. goto err_out;
  150. err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
  151. if (err)
  152. goto err_out;
  153. return 0;
  154. err_out:
  155. mlxfw_dev->ops->fsm_cancel(mlxfw_dev, fwhandle);
  156. return err;
  157. }
  158. static int mlxfw_flash_components(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
  159. struct mlxfw_mfa2_file *mfa2_file)
  160. {
  161. u32 component_count;
  162. int err;
  163. int i;
  164. err = mlxfw_mfa2_file_component_count(mfa2_file, mlxfw_dev->psid,
  165. mlxfw_dev->psid_size,
  166. &component_count);
  167. if (err) {
  168. pr_err("Could not find device PSID in MFA2 file\n");
  169. return err;
  170. }
  171. for (i = 0; i < component_count; i++) {
  172. struct mlxfw_mfa2_component *comp;
  173. comp = mlxfw_mfa2_file_component_get(mfa2_file, mlxfw_dev->psid,
  174. mlxfw_dev->psid_size, i);
  175. if (IS_ERR(comp))
  176. return PTR_ERR(comp);
  177. pr_info("Flashing component type %d\n", comp->index);
  178. err = mlxfw_flash_component(mlxfw_dev, fwhandle, comp);
  179. mlxfw_mfa2_file_component_put(comp);
  180. if (err)
  181. return err;
  182. }
  183. return 0;
  184. }
  185. int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
  186. const struct firmware *firmware)
  187. {
  188. struct mlxfw_mfa2_file *mfa2_file;
  189. u32 fwhandle;
  190. int err;
  191. if (!mlxfw_mfa2_check(firmware)) {
  192. pr_err("Firmware file is not MFA2\n");
  193. return -EINVAL;
  194. }
  195. mfa2_file = mlxfw_mfa2_file_init(firmware);
  196. if (IS_ERR(mfa2_file))
  197. return PTR_ERR(mfa2_file);
  198. pr_info("Initialize firmware flash process\n");
  199. err = mlxfw_dev->ops->fsm_lock(mlxfw_dev, &fwhandle);
  200. if (err) {
  201. pr_err("Could not lock the firmware FSM\n");
  202. goto err_fsm_lock;
  203. }
  204. err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
  205. MLXFW_FSM_STATE_LOCKED);
  206. if (err)
  207. goto err_state_wait_idle_to_locked;
  208. err = mlxfw_flash_components(mlxfw_dev, fwhandle, mfa2_file);
  209. if (err)
  210. goto err_flash_components;
  211. pr_debug("Activate image\n");
  212. err = mlxfw_dev->ops->fsm_activate(mlxfw_dev, fwhandle);
  213. if (err) {
  214. pr_err("Could not activate the downloaded image\n");
  215. goto err_fsm_activate;
  216. }
  217. err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
  218. if (err)
  219. goto err_state_wait_activate_to_locked;
  220. pr_debug("Handle release\n");
  221. mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
  222. pr_info("Firmware flash done.\n");
  223. mlxfw_mfa2_file_fini(mfa2_file);
  224. return 0;
  225. err_state_wait_activate_to_locked:
  226. err_fsm_activate:
  227. err_flash_components:
  228. err_state_wait_idle_to_locked:
  229. mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
  230. err_fsm_lock:
  231. mlxfw_mfa2_file_fini(mfa2_file);
  232. return err;
  233. }
  234. EXPORT_SYMBOL(mlxfw_firmware_flash);
  235. MODULE_LICENSE("Dual BSD/GPL");
  236. MODULE_AUTHOR("Yotam Gigi <yotamg@mellanox.com>");
  237. MODULE_DESCRIPTION("Mellanox firmware flash lib");