• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
mlxfw_fsm_state_wait(struct mlxfw_dev * mlxfw_dev,u32 fwhandle,enum mlxfw_fsm_state fsm_state)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 		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]);
93 		return -EINVAL;
94 	}
95 	if (curr_fsm_state != fsm_state) {
96 		if (--times == 0) {
97 			pr_err("Timeout reached on FSM state change");
98 			return -ETIMEDOUT;
99 		}
100 		msleep(MLXFW_FSM_STATE_WAIT_CYCLE_MS);
101 		goto retry;
102 	}
103 	return 0;
104 }
105 
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))
109 
mlxfw_flash_component(struct mlxfw_dev * mlxfw_dev,u32 fwhandle,struct mlxfw_mfa2_component * comp)110 static int mlxfw_flash_component(struct mlxfw_dev *mlxfw_dev,
111 				 u32 fwhandle,
112 				 struct mlxfw_mfa2_component *comp)
113 {
114 	u16 comp_max_write_size;
115 	u8 comp_align_bits;
116 	u32 comp_max_size;
117 	u16 block_size;
118 	u8 *block_ptr;
119 	u32 offset;
120 	int err;
121 
122 	err = mlxfw_dev->ops->component_query(mlxfw_dev, comp->index,
123 					      &comp_max_size, &comp_align_bits,
124 					      &comp_max_write_size);
125 	if (err)
126 		return err;
127 
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);
132 		return -EINVAL;
133 	}
134 
135 	comp_max_write_size = MLXFW_ALIGN_DOWN(comp_max_write_size,
136 					       comp_align_bits);
137 
138 	pr_debug("Component update\n");
139 	err = mlxfw_dev->ops->fsm_component_update(mlxfw_dev, fwhandle,
140 						   comp->index,
141 						   comp->data_size);
142 	if (err)
143 		return err;
144 
145 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
146 				   MLXFW_FSM_STATE_DOWNLOAD);
147 	if (err)
148 		goto err_out;
149 
150 	pr_debug("Component download\n");
151 	for (offset = 0;
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,
159 							 offset);
160 		if (err)
161 			goto err_out;
162 	}
163 
164 	pr_debug("Component verify\n");
165 	err = mlxfw_dev->ops->fsm_component_verify(mlxfw_dev, fwhandle,
166 						   comp->index);
167 	if (err)
168 		goto err_out;
169 
170 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
171 	if (err)
172 		goto err_out;
173 	return 0;
174 
175 err_out:
176 	mlxfw_dev->ops->fsm_cancel(mlxfw_dev, fwhandle);
177 	return err;
178 }
179 
mlxfw_flash_components(struct mlxfw_dev * mlxfw_dev,u32 fwhandle,struct mlxfw_mfa2_file * mfa2_file)180 static int mlxfw_flash_components(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
181 				  struct mlxfw_mfa2_file *mfa2_file)
182 {
183 	u32 component_count;
184 	int err;
185 	int i;
186 
187 	err = mlxfw_mfa2_file_component_count(mfa2_file, mlxfw_dev->psid,
188 					      mlxfw_dev->psid_size,
189 					      &component_count);
190 	if (err) {
191 		pr_err("Could not find device PSID in MFA2 file\n");
192 		return err;
193 	}
194 
195 	for (i = 0; i < component_count; i++) {
196 		struct mlxfw_mfa2_component *comp;
197 
198 		comp = mlxfw_mfa2_file_component_get(mfa2_file, mlxfw_dev->psid,
199 						     mlxfw_dev->psid_size, i);
200 		if (IS_ERR(comp))
201 			return PTR_ERR(comp);
202 
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);
206 		if (err)
207 			return err;
208 	}
209 	return 0;
210 }
211 
mlxfw_firmware_flash(struct mlxfw_dev * mlxfw_dev,const struct firmware * firmware)212 int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
213 			 const struct firmware *firmware)
214 {
215 	struct mlxfw_mfa2_file *mfa2_file;
216 	u32 fwhandle;
217 	int err;
218 
219 	if (!mlxfw_mfa2_check(firmware)) {
220 		pr_err("Firmware file is not MFA2\n");
221 		return -EINVAL;
222 	}
223 
224 	mfa2_file = mlxfw_mfa2_file_init(firmware);
225 	if (IS_ERR(mfa2_file))
226 		return PTR_ERR(mfa2_file);
227 
228 	pr_info("Initialize firmware flash process\n");
229 	err = mlxfw_dev->ops->fsm_lock(mlxfw_dev, &fwhandle);
230 	if (err) {
231 		pr_err("Could not lock the firmware FSM\n");
232 		goto err_fsm_lock;
233 	}
234 
235 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
236 				   MLXFW_FSM_STATE_LOCKED);
237 	if (err)
238 		goto err_state_wait_idle_to_locked;
239 
240 	err = mlxfw_flash_components(mlxfw_dev, fwhandle, mfa2_file);
241 	if (err)
242 		goto err_flash_components;
243 
244 	pr_debug("Activate image\n");
245 	err = mlxfw_dev->ops->fsm_activate(mlxfw_dev, fwhandle);
246 	if (err) {
247 		pr_err("Could not activate the downloaded image\n");
248 		goto err_fsm_activate;
249 	}
250 
251 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle, MLXFW_FSM_STATE_LOCKED);
252 	if (err)
253 		goto err_state_wait_activate_to_locked;
254 
255 	pr_debug("Handle release\n");
256 	mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
257 
258 	pr_info("Firmware flash done.\n");
259 	mlxfw_mfa2_file_fini(mfa2_file);
260 	return 0;
261 
262 err_state_wait_activate_to_locked:
263 err_fsm_activate:
264 err_flash_components:
265 err_state_wait_idle_to_locked:
266 	mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
267 err_fsm_lock:
268 	mlxfw_mfa2_file_fini(mfa2_file);
269 	return err;
270 }
271 EXPORT_SYMBOL(mlxfw_firmware_flash);
272 
273 MODULE_LICENSE("Dual BSD/GPL");
274 MODULE_AUTHOR("Yotam Gigi <yotamg@mellanox.com>");
275 MODULE_DESCRIPTION("Mellanox firmware flash lib");
276