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 pr_err("Firmware flash failed: %s\n",
90 mlxfw_fsm_state_err_str[fsm_state_err]);
93 if (curr_fsm_state != fsm_state) {
95 pr_err("Timeout reached on FSM state change");
98 msleep(MLXFW_FSM_STATE_WAIT_CYCLE_MS);
104 #define MLXFW_ALIGN_DOWN(x, align_bits) ((x) & ~((1 << (align_bits)) - 1))
105 #define MLXFW_ALIGN_UP(x, align_bits) \
106 MLXFW_ALIGN_DOWN((x) + ((1 << (align_bits)) - 1), (align_bits))
108 static int mlxfw_flash_component(struct mlxfw_dev *mlxfw_dev,
110 struct mlxfw_mfa2_component *comp)
112 u16 comp_max_write_size;
120 err = mlxfw_dev->ops->component_query(mlxfw_dev, comp->index,
121 &comp_max_size, &comp_align_bits,
122 &comp_max_write_size);
126 comp_max_size = min_t(u32, comp_max_size, MLXFW_FSM_MAX_COMPONENT_SIZE);
127 if (comp->data_size > comp_max_size) {
128 pr_err("Component %d is of size %d which is bigger than limit %d\n",
129 comp->index, comp->data_size, comp_max_size);
133 comp_max_write_size = MLXFW_ALIGN_DOWN(comp_max_write_size,
136 pr_debug("Component update\n");
137 err = mlxfw_dev->ops->fsm_component_update(mlxfw_dev, fwhandle,
143 err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
144 MLXFW_FSM_STATE_DOWNLOAD);
148 pr_debug("Component download\n");
150 offset < MLXFW_ALIGN_UP(comp->data_size, comp_align_bits);
151 offset += comp_max_write_size) {
152 block_ptr = comp->data + offset;
153 block_size = (u16) min_t(u32, comp->data_size - offset,
154 comp_max_write_size);
155 err = mlxfw_dev->ops->fsm_block_download(mlxfw_dev, fwhandle,
156 block_ptr, block_size,
162 pr_debug("Component verify\n");
163 err = mlxfw_dev->ops->fsm_component_verify(mlxfw_dev, fwhandle,
168 err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
174 mlxfw_dev->ops->fsm_cancel(mlxfw_dev, fwhandle);
178 static int mlxfw_flash_components(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
179 struct mlxfw_mfa2_file *mfa2_file)
185 err = mlxfw_mfa2_file_component_count(mfa2_file, mlxfw_dev->psid,
186 mlxfw_dev->psid_size,
189 pr_err("Could not find device PSID in MFA2 file\n");
193 for (i = 0; i < component_count; i++) {
194 struct mlxfw_mfa2_component *comp;
196 comp = mlxfw_mfa2_file_component_get(mfa2_file, mlxfw_dev->psid,
197 mlxfw_dev->psid_size, i);
199 return PTR_ERR(comp);
201 pr_info("Flashing component type %d\n", comp->index);
202 err = mlxfw_flash_component(mlxfw_dev, fwhandle, comp);
203 mlxfw_mfa2_file_component_put(comp);
210 int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
211 const struct firmware *firmware)
213 struct mlxfw_mfa2_file *mfa2_file;
217 if (!mlxfw_mfa2_check(firmware)) {
218 pr_err("Firmware file is not MFA2\n");
222 mfa2_file = mlxfw_mfa2_file_init(firmware);
223 if (IS_ERR(mfa2_file))
224 return PTR_ERR(mfa2_file);
226 pr_info("Initialize firmware flash process\n");
227 err = mlxfw_dev->ops->fsm_lock(mlxfw_dev, &fwhandle);
229 pr_err("Could not lock the firmware FSM\n");
233 err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
234 MLXFW_FSM_STATE_LOCKED);
236 goto err_state_wait_idle_to_locked;
238 err = mlxfw_flash_components(mlxfw_dev, fwhandle, mfa2_file);
240 goto err_flash_components;
242 pr_debug("Activate image\n");
243 err = mlxfw_dev->ops->fsm_activate(mlxfw_dev, fwhandle);
245 pr_err("Could not activate the downloaded image\n");
246 goto err_fsm_activate;
249 err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
251 goto err_state_wait_activate_to_locked;
253 pr_debug("Handle release\n");
254 mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
256 pr_info("Firmware flash done.\n");
257 mlxfw_mfa2_file_fini(mfa2_file);
260 err_state_wait_activate_to_locked:
262 err_flash_components:
263 err_state_wait_idle_to_locked:
264 mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
266 mlxfw_mfa2_file_fini(mfa2_file);
269 EXPORT_SYMBOL(mlxfw_firmware_flash);
271 MODULE_LICENSE("Dual BSD/GPL");
272 MODULE_AUTHOR("Yotam Gigi <yotamg@mellanox.com>");
273 MODULE_DESCRIPTION("Mellanox firmware flash lib");