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 
35 #define pr_fmt(fmt) "mlxfw: " fmt
36 
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 
41 #include "mlxfw.h"
42 #include "mlxfw_mfa2.h"
43 
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))
49 
50 static const char * const mlxfw_fsm_state_err_str[] = {
51 	[MLXFW_FSM_STATE_ERR_ERROR] =
52 		"general 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] =
58 		"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] =
64 		"key not applicable",
65 	[MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT] =
66 		"bad format",
67 	[MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET] =
68 		"pending reset",
69 	[MLXFW_FSM_STATE_ERR_MAX] =
70 		"unknown error"
71 };
72 
73 static int mlxfw_fsm_state_wait(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
74 				enum mlxfw_fsm_state fsm_state)
75 {
76 	enum mlxfw_fsm_state_err fsm_state_err;
77 	enum mlxfw_fsm_state curr_fsm_state;
78 	int times;
79 	int err;
80 
81 	times = MLXFW_FSM_STATE_WAIT_ROUNDS;
82 retry:
83 	err = mlxfw_dev->ops->fsm_query_state(mlxfw_dev, fwhandle,
84 					      &curr_fsm_state, &fsm_state_err);
85 	if (err)
86 		return err;
87 
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]);
91 		return -EINVAL;
92 	}
93 	if (curr_fsm_state != fsm_state) {
94 		if (--times == 0) {
95 			pr_err("Timeout reached on FSM state change");
96 			return -ETIMEDOUT;
97 		}
98 		msleep(MLXFW_FSM_STATE_WAIT_CYCLE_MS);
99 		goto retry;
100 	}
101 	return 0;
102 }
103 
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))
107 
108 static int mlxfw_flash_component(struct mlxfw_dev *mlxfw_dev,
109 				 u32 fwhandle,
110 				 struct mlxfw_mfa2_component *comp)
111 {
112 	u16 comp_max_write_size;
113 	u8 comp_align_bits;
114 	u32 comp_max_size;
115 	u16 block_size;
116 	u8 *block_ptr;
117 	u32 offset;
118 	int err;
119 
120 	err = mlxfw_dev->ops->component_query(mlxfw_dev, comp->index,
121 					      &comp_max_size, &comp_align_bits,
122 					      &comp_max_write_size);
123 	if (err)
124 		return err;
125 
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);
130 		return -EINVAL;
131 	}
132 
133 	comp_max_write_size = MLXFW_ALIGN_DOWN(comp_max_write_size,
134 					       comp_align_bits);
135 
136 	pr_debug("Component update\n");
137 	err = mlxfw_dev->ops->fsm_component_update(mlxfw_dev, fwhandle,
138 						   comp->index,
139 						   comp->data_size);
140 	if (err)
141 		return err;
142 
143 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
144 				   MLXFW_FSM_STATE_DOWNLOAD);
145 	if (err)
146 		goto err_out;
147 
148 	pr_debug("Component download\n");
149 	for (offset = 0;
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,
157 							 offset);
158 		if (err)
159 			goto err_out;
160 	}
161 
162 	pr_debug("Component verify\n");
163 	err = mlxfw_dev->ops->fsm_component_verify(mlxfw_dev, fwhandle,
164 						   comp->index);
165 	if (err)
166 		goto err_out;
167 
168 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
169 	if (err)
170 		goto err_out;
171 	return 0;
172 
173 err_out:
174 	mlxfw_dev->ops->fsm_cancel(mlxfw_dev, fwhandle);
175 	return err;
176 }
177 
178 static int mlxfw_flash_components(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
179 				  struct mlxfw_mfa2_file *mfa2_file)
180 {
181 	u32 component_count;
182 	int err;
183 	int i;
184 
185 	err = mlxfw_mfa2_file_component_count(mfa2_file, mlxfw_dev->psid,
186 					      mlxfw_dev->psid_size,
187 					      &component_count);
188 	if (err) {
189 		pr_err("Could not find device PSID in MFA2 file\n");
190 		return err;
191 	}
192 
193 	for (i = 0; i < component_count; i++) {
194 		struct mlxfw_mfa2_component *comp;
195 
196 		comp = mlxfw_mfa2_file_component_get(mfa2_file, mlxfw_dev->psid,
197 						     mlxfw_dev->psid_size, i);
198 		if (IS_ERR(comp))
199 			return PTR_ERR(comp);
200 
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);
204 		if (err)
205 			return err;
206 	}
207 	return 0;
208 }
209 
210 int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
211 			 const struct firmware *firmware)
212 {
213 	struct mlxfw_mfa2_file *mfa2_file;
214 	u32 fwhandle;
215 	int err;
216 
217 	if (!mlxfw_mfa2_check(firmware)) {
218 		pr_err("Firmware file is not MFA2\n");
219 		return -EINVAL;
220 	}
221 
222 	mfa2_file = mlxfw_mfa2_file_init(firmware);
223 	if (IS_ERR(mfa2_file))
224 		return PTR_ERR(mfa2_file);
225 
226 	pr_info("Initialize firmware flash process\n");
227 	err = mlxfw_dev->ops->fsm_lock(mlxfw_dev, &fwhandle);
228 	if (err) {
229 		pr_err("Could not lock the firmware FSM\n");
230 		goto err_fsm_lock;
231 	}
232 
233 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
234 				   MLXFW_FSM_STATE_LOCKED);
235 	if (err)
236 		goto err_state_wait_idle_to_locked;
237 
238 	err = mlxfw_flash_components(mlxfw_dev, fwhandle, mfa2_file);
239 	if (err)
240 		goto err_flash_components;
241 
242 	pr_debug("Activate image\n");
243 	err = mlxfw_dev->ops->fsm_activate(mlxfw_dev, fwhandle);
244 	if (err) {
245 		pr_err("Could not activate the downloaded image\n");
246 		goto err_fsm_activate;
247 	}
248 
249 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
250 	if (err)
251 		goto err_state_wait_activate_to_locked;
252 
253 	pr_debug("Handle release\n");
254 	mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
255 
256 	pr_info("Firmware flash done.\n");
257 	mlxfw_mfa2_file_fini(mfa2_file);
258 	return 0;
259 
260 err_state_wait_activate_to_locked:
261 err_fsm_activate:
262 err_flash_components:
263 err_state_wait_idle_to_locked:
264 	mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
265 err_fsm_lock:
266 	mlxfw_mfa2_file_fini(mfa2_file);
267 	return err;
268 }
269 EXPORT_SYMBOL(mlxfw_firmware_flash);
270 
271 MODULE_LICENSE("Dual BSD/GPL");
272 MODULE_AUTHOR("Yotam Gigi <yotamg@mellanox.com>");
273 MODULE_DESCRIPTION("Mellanox firmware flash lib");
274