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>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
35 #define pr_fmt(fmt) "mlxfw: " fmt
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/delay.h>
42 #include "mlxfw_mfa2.h"
44 #define MLXFW_FSM_STATE_WAIT_CYCLE_MS 200
45 #define MLXFW_FSM_STATE_WAIT_TIMEOUT_MS 30000
46 #define MLXFW_FSM_STATE_WAIT_ROUNDS \
47 (MLXFW_FSM_STATE_WAIT_TIMEOUT_MS / MLXFW_FSM_STATE_WAIT_CYCLE_MS)
48 #define MLXFW_FSM_MAX_COMPONENT_SIZE (10 * (1 << 20))
50 static const char * const mlxfw_fsm_state_err_str[] = {
51 [MLXFW_FSM_STATE_ERR_ERROR] =
53 [MLXFW_FSM_STATE_ERR_REJECTED_DIGEST_ERR] =
54 "component hash mismatch",
55 [MLXFW_FSM_STATE_ERR_REJECTED_NOT_APPLICABLE] =
56 "component not applicable",
57 [MLXFW_FSM_STATE_ERR_REJECTED_UNKNOWN_KEY] =
59 [MLXFW_FSM_STATE_ERR_REJECTED_AUTH_FAILED] =
60 "authentication failed",
61 [MLXFW_FSM_STATE_ERR_REJECTED_UNSIGNED] =
62 "component was not signed",
63 [MLXFW_FSM_STATE_ERR_REJECTED_KEY_NOT_APPLICABLE] =
65 [MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT] =
67 [MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET] =
69 [MLXFW_FSM_STATE_ERR_MAX] =
73 static int mlxfw_fsm_state_wait(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
74 enum mlxfw_fsm_state fsm_state)
76 enum mlxfw_fsm_state_err fsm_state_err;
77 enum mlxfw_fsm_state curr_fsm_state;
81 times = MLXFW_FSM_STATE_WAIT_ROUNDS;
83 err = mlxfw_dev->ops->fsm_query_state(mlxfw_dev, fwhandle,
84 &curr_fsm_state, &fsm_state_err);
88 if (fsm_state_err != MLXFW_FSM_STATE_ERR_OK) {
89 fsm_state_err = min_t(enum mlxfw_fsm_state_err,
90 fsm_state_err, MLXFW_FSM_STATE_ERR_MAX);
91 pr_err("Firmware flash failed: %s\n",
92 mlxfw_fsm_state_err_str[fsm_state_err]);
95 if (curr_fsm_state != fsm_state) {
97 pr_err("Timeout reached on FSM state change");
100 msleep(MLXFW_FSM_STATE_WAIT_CYCLE_MS);
106 #define MLXFW_ALIGN_DOWN(x, align_bits) ((x) & ~((1 << (align_bits)) - 1))
107 #define MLXFW_ALIGN_UP(x, align_bits) \
108 MLXFW_ALIGN_DOWN((x) + ((1 << (align_bits)) - 1), (align_bits))
110 static int mlxfw_flash_component(struct mlxfw_dev *mlxfw_dev,
112 struct mlxfw_mfa2_component *comp)
114 u16 comp_max_write_size;
122 err = mlxfw_dev->ops->component_query(mlxfw_dev, comp->index,
123 &comp_max_size, &comp_align_bits,
124 &comp_max_write_size);
128 comp_max_size = min_t(u32, comp_max_size, MLXFW_FSM_MAX_COMPONENT_SIZE);
129 if (comp->data_size > comp_max_size) {
130 pr_err("Component %d is of size %d which is bigger than limit %d\n",
131 comp->index, comp->data_size, comp_max_size);
135 comp_max_write_size = MLXFW_ALIGN_DOWN(comp_max_write_size,
138 pr_debug("Component update\n");
139 err = mlxfw_dev->ops->fsm_component_update(mlxfw_dev, fwhandle,
145 err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
146 MLXFW_FSM_STATE_DOWNLOAD);
150 pr_debug("Component download\n");
152 offset < MLXFW_ALIGN_UP(comp->data_size, comp_align_bits);
153 offset += comp_max_write_size) {
154 block_ptr = comp->data + offset;
155 block_size = (u16) min_t(u32, comp->data_size - offset,
156 comp_max_write_size);
157 err = mlxfw_dev->ops->fsm_block_download(mlxfw_dev, fwhandle,
158 block_ptr, block_size,
164 pr_debug("Component verify\n");
165 err = mlxfw_dev->ops->fsm_component_verify(mlxfw_dev, fwhandle,
170 err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
176 mlxfw_dev->ops->fsm_cancel(mlxfw_dev, fwhandle);
180 static int mlxfw_flash_components(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
181 struct mlxfw_mfa2_file *mfa2_file)
187 err = mlxfw_mfa2_file_component_count(mfa2_file, mlxfw_dev->psid,
188 mlxfw_dev->psid_size,
191 pr_err("Could not find device PSID in MFA2 file\n");
195 for (i = 0; i < component_count; i++) {
196 struct mlxfw_mfa2_component *comp;
198 comp = mlxfw_mfa2_file_component_get(mfa2_file, mlxfw_dev->psid,
199 mlxfw_dev->psid_size, i);
201 return PTR_ERR(comp);
203 pr_info("Flashing component type %d\n", comp->index);
204 err = mlxfw_flash_component(mlxfw_dev, fwhandle, comp);
205 mlxfw_mfa2_file_component_put(comp);
212 int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
213 const struct firmware *firmware)
215 struct mlxfw_mfa2_file *mfa2_file;
219 if (!mlxfw_mfa2_check(firmware)) {
220 pr_err("Firmware file is not MFA2\n");
224 mfa2_file = mlxfw_mfa2_file_init(firmware);
225 if (IS_ERR(mfa2_file))
226 return PTR_ERR(mfa2_file);
228 pr_info("Initialize firmware flash process\n");
229 err = mlxfw_dev->ops->fsm_lock(mlxfw_dev, &fwhandle);
231 pr_err("Could not lock the firmware FSM\n");
235 err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
236 MLXFW_FSM_STATE_LOCKED);
238 goto err_state_wait_idle_to_locked;
240 err = mlxfw_flash_components(mlxfw_dev, fwhandle, mfa2_file);
242 goto err_flash_components;
244 pr_debug("Activate image\n");
245 err = mlxfw_dev->ops->fsm_activate(mlxfw_dev, fwhandle);
247 pr_err("Could not activate the downloaded image\n");
248 goto err_fsm_activate;
251 err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
253 goto err_state_wait_activate_to_locked;
255 pr_debug("Handle release\n");
256 mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
258 pr_info("Firmware flash done.\n");
259 mlxfw_mfa2_file_fini(mfa2_file);
262 err_state_wait_activate_to_locked:
264 err_flash_components:
265 err_state_wait_idle_to_locked:
266 mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
268 mlxfw_mfa2_file_fini(mfa2_file);
271 EXPORT_SYMBOL(mlxfw_firmware_flash);
273 MODULE_LICENSE("Dual BSD/GPL");
274 MODULE_AUTHOR("Yotam Gigi <yotamg@mellanox.com>");
275 MODULE_DESCRIPTION("Mellanox firmware flash lib");