18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2017-2019 Mellanox Technologies. All rights reserved */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "mlxfw: " fmt
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/delay.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include "mlxfw.h"
118c2ecf20Sopenharmony_ci#include "mlxfw_mfa2.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define MLXFW_FSM_STATE_WAIT_CYCLE_MS 200
148c2ecf20Sopenharmony_ci#define MLXFW_FSM_STATE_WAIT_TIMEOUT_MS 30000
158c2ecf20Sopenharmony_ci#define MLXFW_FSM_STATE_WAIT_ROUNDS \
168c2ecf20Sopenharmony_ci	(MLXFW_FSM_STATE_WAIT_TIMEOUT_MS / MLXFW_FSM_STATE_WAIT_CYCLE_MS)
178c2ecf20Sopenharmony_ci#define MLXFW_FSM_MAX_COMPONENT_SIZE (10 * (1 << 20))
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic const int mlxfw_fsm_state_errno[] = {
208c2ecf20Sopenharmony_ci	[MLXFW_FSM_STATE_ERR_ERROR] = -EIO,
218c2ecf20Sopenharmony_ci	[MLXFW_FSM_STATE_ERR_REJECTED_DIGEST_ERR] = -EBADMSG,
228c2ecf20Sopenharmony_ci	[MLXFW_FSM_STATE_ERR_REJECTED_NOT_APPLICABLE] = -ENOENT,
238c2ecf20Sopenharmony_ci	[MLXFW_FSM_STATE_ERR_REJECTED_UNKNOWN_KEY] = -ENOKEY,
248c2ecf20Sopenharmony_ci	[MLXFW_FSM_STATE_ERR_REJECTED_AUTH_FAILED] = -EACCES,
258c2ecf20Sopenharmony_ci	[MLXFW_FSM_STATE_ERR_REJECTED_UNSIGNED] = -EKEYREVOKED,
268c2ecf20Sopenharmony_ci	[MLXFW_FSM_STATE_ERR_REJECTED_KEY_NOT_APPLICABLE] = -EKEYREJECTED,
278c2ecf20Sopenharmony_ci	[MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT] = -ENOEXEC,
288c2ecf20Sopenharmony_ci	[MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET] = -EBUSY,
298c2ecf20Sopenharmony_ci	[MLXFW_FSM_STATE_ERR_MAX] = -EINVAL
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define MLXFW_ERR_PRFX "Firmware flash failed: "
338c2ecf20Sopenharmony_ci#define MLXFW_ERR_MSG(fwdev, extack, msg, err) do { \
348c2ecf20Sopenharmony_ci	mlxfw_err(fwdev, "%s, err (%d)\n", MLXFW_ERR_PRFX msg, err); \
358c2ecf20Sopenharmony_ci	NL_SET_ERR_MSG_MOD(extack, MLXFW_ERR_PRFX msg); \
368c2ecf20Sopenharmony_ci} while (0)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int mlxfw_fsm_state_err(struct mlxfw_dev *mlxfw_dev,
398c2ecf20Sopenharmony_ci			       struct netlink_ext_ack *extack,
408c2ecf20Sopenharmony_ci			       enum mlxfw_fsm_state_err err)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	enum mlxfw_fsm_state_err fsm_state_err;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	fsm_state_err = min_t(enum mlxfw_fsm_state_err, err,
458c2ecf20Sopenharmony_ci			      MLXFW_FSM_STATE_ERR_MAX);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	switch (fsm_state_err) {
488c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_ERROR:
498c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "general error", err);
508c2ecf20Sopenharmony_ci		break;
518c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_REJECTED_DIGEST_ERR:
528c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "component hash mismatch", err);
538c2ecf20Sopenharmony_ci		break;
548c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_REJECTED_NOT_APPLICABLE:
558c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "component not applicable", err);
568c2ecf20Sopenharmony_ci		break;
578c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_REJECTED_UNKNOWN_KEY:
588c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "unknown key", err);
598c2ecf20Sopenharmony_ci		break;
608c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_REJECTED_AUTH_FAILED:
618c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "authentication failed", err);
628c2ecf20Sopenharmony_ci		break;
638c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_REJECTED_UNSIGNED:
648c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "component was not signed", err);
658c2ecf20Sopenharmony_ci		break;
668c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_REJECTED_KEY_NOT_APPLICABLE:
678c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "key not applicable", err);
688c2ecf20Sopenharmony_ci		break;
698c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT:
708c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "bad format", err);
718c2ecf20Sopenharmony_ci		break;
728c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET:
738c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "pending reset", err);
748c2ecf20Sopenharmony_ci		break;
758c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_OK:
768c2ecf20Sopenharmony_ci	case MLXFW_FSM_STATE_ERR_MAX:
778c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "unknown error", err);
788c2ecf20Sopenharmony_ci		break;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return mlxfw_fsm_state_errno[fsm_state_err];
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int mlxfw_fsm_state_wait(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
858c2ecf20Sopenharmony_ci				enum mlxfw_fsm_state fsm_state,
868c2ecf20Sopenharmony_ci				struct netlink_ext_ack *extack)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	enum mlxfw_fsm_state_err fsm_state_err;
898c2ecf20Sopenharmony_ci	enum mlxfw_fsm_state curr_fsm_state;
908c2ecf20Sopenharmony_ci	int times;
918c2ecf20Sopenharmony_ci	int err;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	times = MLXFW_FSM_STATE_WAIT_ROUNDS;
948c2ecf20Sopenharmony_ciretry:
958c2ecf20Sopenharmony_ci	err = mlxfw_dev->ops->fsm_query_state(mlxfw_dev, fwhandle,
968c2ecf20Sopenharmony_ci					      &curr_fsm_state, &fsm_state_err);
978c2ecf20Sopenharmony_ci	if (err) {
988c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "FSM state query failed", err);
998c2ecf20Sopenharmony_ci		return err;
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	if (fsm_state_err != MLXFW_FSM_STATE_ERR_OK)
1038c2ecf20Sopenharmony_ci		return mlxfw_fsm_state_err(mlxfw_dev, extack, fsm_state_err);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (curr_fsm_state != fsm_state) {
1068c2ecf20Sopenharmony_ci		if (--times == 0) {
1078c2ecf20Sopenharmony_ci			MLXFW_ERR_MSG(mlxfw_dev, extack,
1088c2ecf20Sopenharmony_ci				      "Timeout reached on FSM state change", -ETIMEDOUT);
1098c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
1108c2ecf20Sopenharmony_ci		}
1118c2ecf20Sopenharmony_ci		msleep(MLXFW_FSM_STATE_WAIT_CYCLE_MS);
1128c2ecf20Sopenharmony_ci		goto retry;
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci	return 0;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int
1188c2ecf20Sopenharmony_cimlxfw_fsm_reactivate_err(struct mlxfw_dev *mlxfw_dev,
1198c2ecf20Sopenharmony_ci			 struct netlink_ext_ack *extack, u8 err)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	enum mlxfw_fsm_reactivate_status status;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci#define MXFW_REACT_PRFX "Reactivate FSM: "
1248c2ecf20Sopenharmony_ci#define MLXFW_REACT_ERR(msg, err) \
1258c2ecf20Sopenharmony_ci	MLXFW_ERR_MSG(mlxfw_dev, extack, MXFW_REACT_PRFX msg, err)
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	status = min_t(enum mlxfw_fsm_reactivate_status, err,
1288c2ecf20Sopenharmony_ci		       MLXFW_FSM_REACTIVATE_STATUS_MAX);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	switch (status) {
1318c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_BUSY:
1328c2ecf20Sopenharmony_ci		MLXFW_REACT_ERR("busy", err);
1338c2ecf20Sopenharmony_ci		break;
1348c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_PROHIBITED_FW_VER_ERR:
1358c2ecf20Sopenharmony_ci		MLXFW_REACT_ERR("prohibited fw ver", err);
1368c2ecf20Sopenharmony_ci		break;
1378c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_COPY_FAILED:
1388c2ecf20Sopenharmony_ci		MLXFW_REACT_ERR("first page copy failed", err);
1398c2ecf20Sopenharmony_ci		break;
1408c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_ERASE_FAILED:
1418c2ecf20Sopenharmony_ci		MLXFW_REACT_ERR("first page erase failed", err);
1428c2ecf20Sopenharmony_ci		break;
1438c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_RESTORE_FAILED:
1448c2ecf20Sopenharmony_ci		MLXFW_REACT_ERR("first page restore failed", err);
1458c2ecf20Sopenharmony_ci		break;
1468c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_CANDIDATE_FW_DEACTIVATION_FAILED:
1478c2ecf20Sopenharmony_ci		MLXFW_REACT_ERR("candidate fw deactivation failed", err);
1488c2ecf20Sopenharmony_ci		break;
1498c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_ERR_DEVICE_RESET_REQUIRED:
1508c2ecf20Sopenharmony_ci		MLXFW_REACT_ERR("device reset required", err);
1518c2ecf20Sopenharmony_ci		break;
1528c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_ERR_FW_PROGRAMMING_NEEDED:
1538c2ecf20Sopenharmony_ci		MLXFW_REACT_ERR("fw programming needed", err);
1548c2ecf20Sopenharmony_ci		break;
1558c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_FW_ALREADY_ACTIVATED:
1568c2ecf20Sopenharmony_ci		MLXFW_REACT_ERR("fw already activated", err);
1578c2ecf20Sopenharmony_ci		break;
1588c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_OK:
1598c2ecf20Sopenharmony_ci	case MLXFW_FSM_REACTIVATE_STATUS_MAX:
1608c2ecf20Sopenharmony_ci		MLXFW_REACT_ERR("unexpected error", err);
1618c2ecf20Sopenharmony_ci		break;
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci	return -EREMOTEIO;
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic int mlxfw_fsm_reactivate(struct mlxfw_dev *mlxfw_dev,
1678c2ecf20Sopenharmony_ci				struct netlink_ext_ack *extack,
1688c2ecf20Sopenharmony_ci				bool *supported)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	u8 status;
1718c2ecf20Sopenharmony_ci	int err;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (!mlxfw_dev->ops->fsm_reactivate)
1748c2ecf20Sopenharmony_ci		return 0;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	err = mlxfw_dev->ops->fsm_reactivate(mlxfw_dev, &status);
1778c2ecf20Sopenharmony_ci	if (err == -EOPNOTSUPP) {
1788c2ecf20Sopenharmony_ci		*supported = false;
1798c2ecf20Sopenharmony_ci		return 0;
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	if (err) {
1838c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack,
1848c2ecf20Sopenharmony_ci			      "Could not reactivate firmware flash", err);
1858c2ecf20Sopenharmony_ci		return err;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (status == MLXFW_FSM_REACTIVATE_STATUS_OK ||
1898c2ecf20Sopenharmony_ci	    status == MLXFW_FSM_REACTIVATE_STATUS_FW_ALREADY_ACTIVATED)
1908c2ecf20Sopenharmony_ci		return 0;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	return mlxfw_fsm_reactivate_err(mlxfw_dev, extack, status);
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic void mlxfw_status_notify(struct mlxfw_dev *mlxfw_dev,
1968c2ecf20Sopenharmony_ci				const char *msg, const char *comp_name,
1978c2ecf20Sopenharmony_ci				u32 done_bytes, u32 total_bytes)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	devlink_flash_update_status_notify(mlxfw_dev->devlink, msg, comp_name,
2008c2ecf20Sopenharmony_ci					   done_bytes, total_bytes);
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci#define MLXFW_ALIGN_DOWN(x, align_bits) ((x) & ~((1 << (align_bits)) - 1))
2048c2ecf20Sopenharmony_ci#define MLXFW_ALIGN_UP(x, align_bits) \
2058c2ecf20Sopenharmony_ci		MLXFW_ALIGN_DOWN((x) + ((1 << (align_bits)) - 1), (align_bits))
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic int mlxfw_flash_component(struct mlxfw_dev *mlxfw_dev,
2088c2ecf20Sopenharmony_ci				 u32 fwhandle,
2098c2ecf20Sopenharmony_ci				 struct mlxfw_mfa2_component *comp,
2108c2ecf20Sopenharmony_ci				 bool reactivate_supp,
2118c2ecf20Sopenharmony_ci				 struct netlink_ext_ack *extack)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	u16 comp_max_write_size;
2148c2ecf20Sopenharmony_ci	u8 comp_align_bits;
2158c2ecf20Sopenharmony_ci	u32 comp_max_size;
2168c2ecf20Sopenharmony_ci	char comp_name[8];
2178c2ecf20Sopenharmony_ci	u16 block_size;
2188c2ecf20Sopenharmony_ci	u8 *block_ptr;
2198c2ecf20Sopenharmony_ci	u32 offset;
2208c2ecf20Sopenharmony_ci	int err;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	sprintf(comp_name, "%u", comp->index);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	err = mlxfw_dev->ops->component_query(mlxfw_dev, comp->index,
2258c2ecf20Sopenharmony_ci					      &comp_max_size, &comp_align_bits,
2268c2ecf20Sopenharmony_ci					      &comp_max_write_size);
2278c2ecf20Sopenharmony_ci	if (err) {
2288c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack, "FSM component query failed", err);
2298c2ecf20Sopenharmony_ci		return err;
2308c2ecf20Sopenharmony_ci	}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	comp_max_size = min_t(u32, comp_max_size, MLXFW_FSM_MAX_COMPONENT_SIZE);
2338c2ecf20Sopenharmony_ci	if (comp->data_size > comp_max_size) {
2348c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack,
2358c2ecf20Sopenharmony_ci			      "Component size is bigger than limit", -EINVAL);
2368c2ecf20Sopenharmony_ci		return -EINVAL;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	comp_max_write_size = MLXFW_ALIGN_DOWN(comp_max_write_size,
2408c2ecf20Sopenharmony_ci					       comp_align_bits);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	mlxfw_dbg(mlxfw_dev, "Component update\n");
2438c2ecf20Sopenharmony_ci	mlxfw_status_notify(mlxfw_dev, "Updating component", comp_name, 0, 0);
2448c2ecf20Sopenharmony_ci	err = mlxfw_dev->ops->fsm_component_update(mlxfw_dev, fwhandle,
2458c2ecf20Sopenharmony_ci						   comp->index,
2468c2ecf20Sopenharmony_ci						   comp->data_size);
2478c2ecf20Sopenharmony_ci	if (err) {
2488c2ecf20Sopenharmony_ci		if (!reactivate_supp)
2498c2ecf20Sopenharmony_ci			MLXFW_ERR_MSG(mlxfw_dev, extack,
2508c2ecf20Sopenharmony_ci				      "FSM component update failed, FW reactivate is not supported",
2518c2ecf20Sopenharmony_ci				      err);
2528c2ecf20Sopenharmony_ci		else
2538c2ecf20Sopenharmony_ci			MLXFW_ERR_MSG(mlxfw_dev, extack,
2548c2ecf20Sopenharmony_ci				      "FSM component update failed", err);
2558c2ecf20Sopenharmony_ci		return err;
2568c2ecf20Sopenharmony_ci	}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
2598c2ecf20Sopenharmony_ci				   MLXFW_FSM_STATE_DOWNLOAD, extack);
2608c2ecf20Sopenharmony_ci	if (err)
2618c2ecf20Sopenharmony_ci		goto err_out;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	mlxfw_dbg(mlxfw_dev, "Component download\n");
2648c2ecf20Sopenharmony_ci	mlxfw_status_notify(mlxfw_dev, "Downloading component",
2658c2ecf20Sopenharmony_ci			    comp_name, 0, comp->data_size);
2668c2ecf20Sopenharmony_ci	for (offset = 0;
2678c2ecf20Sopenharmony_ci	     offset < MLXFW_ALIGN_UP(comp->data_size, comp_align_bits);
2688c2ecf20Sopenharmony_ci	     offset += comp_max_write_size) {
2698c2ecf20Sopenharmony_ci		block_ptr = comp->data + offset;
2708c2ecf20Sopenharmony_ci		block_size = (u16) min_t(u32, comp->data_size - offset,
2718c2ecf20Sopenharmony_ci					 comp_max_write_size);
2728c2ecf20Sopenharmony_ci		err = mlxfw_dev->ops->fsm_block_download(mlxfw_dev, fwhandle,
2738c2ecf20Sopenharmony_ci							 block_ptr, block_size,
2748c2ecf20Sopenharmony_ci							 offset);
2758c2ecf20Sopenharmony_ci		if (err) {
2768c2ecf20Sopenharmony_ci			MLXFW_ERR_MSG(mlxfw_dev, extack,
2778c2ecf20Sopenharmony_ci				      "Component download failed", err);
2788c2ecf20Sopenharmony_ci			goto err_out;
2798c2ecf20Sopenharmony_ci		}
2808c2ecf20Sopenharmony_ci		mlxfw_status_notify(mlxfw_dev, "Downloading component",
2818c2ecf20Sopenharmony_ci				    comp_name, offset + block_size,
2828c2ecf20Sopenharmony_ci				    comp->data_size);
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	mlxfw_dbg(mlxfw_dev, "Component verify\n");
2868c2ecf20Sopenharmony_ci	mlxfw_status_notify(mlxfw_dev, "Verifying component", comp_name, 0, 0);
2878c2ecf20Sopenharmony_ci	err = mlxfw_dev->ops->fsm_component_verify(mlxfw_dev, fwhandle,
2888c2ecf20Sopenharmony_ci						   comp->index);
2898c2ecf20Sopenharmony_ci	if (err) {
2908c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack,
2918c2ecf20Sopenharmony_ci			      "FSM component verify failed", err);
2928c2ecf20Sopenharmony_ci		goto err_out;
2938c2ecf20Sopenharmony_ci	}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
2968c2ecf20Sopenharmony_ci				   MLXFW_FSM_STATE_LOCKED, extack);
2978c2ecf20Sopenharmony_ci	if (err)
2988c2ecf20Sopenharmony_ci		goto err_out;
2998c2ecf20Sopenharmony_ci	return 0;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cierr_out:
3028c2ecf20Sopenharmony_ci	mlxfw_dev->ops->fsm_cancel(mlxfw_dev, fwhandle);
3038c2ecf20Sopenharmony_ci	return err;
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic int mlxfw_flash_components(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
3078c2ecf20Sopenharmony_ci				  struct mlxfw_mfa2_file *mfa2_file,
3088c2ecf20Sopenharmony_ci				  bool reactivate_supp,
3098c2ecf20Sopenharmony_ci				  struct netlink_ext_ack *extack)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	u32 component_count;
3128c2ecf20Sopenharmony_ci	int err;
3138c2ecf20Sopenharmony_ci	int i;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	err = mlxfw_mfa2_file_component_count(mfa2_file, mlxfw_dev->psid,
3168c2ecf20Sopenharmony_ci					      mlxfw_dev->psid_size,
3178c2ecf20Sopenharmony_ci					      &component_count);
3188c2ecf20Sopenharmony_ci	if (err) {
3198c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack,
3208c2ecf20Sopenharmony_ci			      "Could not find device PSID in MFA2 file", err);
3218c2ecf20Sopenharmony_ci		return err;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	for (i = 0; i < component_count; i++) {
3258c2ecf20Sopenharmony_ci		struct mlxfw_mfa2_component *comp;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci		comp = mlxfw_mfa2_file_component_get(mfa2_file, mlxfw_dev->psid,
3288c2ecf20Sopenharmony_ci						     mlxfw_dev->psid_size, i);
3298c2ecf20Sopenharmony_ci		if (IS_ERR(comp)) {
3308c2ecf20Sopenharmony_ci			err = PTR_ERR(comp);
3318c2ecf20Sopenharmony_ci			MLXFW_ERR_MSG(mlxfw_dev, extack,
3328c2ecf20Sopenharmony_ci				      "Failed to get MFA2 component", err);
3338c2ecf20Sopenharmony_ci			return err;
3348c2ecf20Sopenharmony_ci		}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci		mlxfw_info(mlxfw_dev, "Flashing component type %d\n",
3378c2ecf20Sopenharmony_ci			   comp->index);
3388c2ecf20Sopenharmony_ci		err = mlxfw_flash_component(mlxfw_dev, fwhandle, comp,
3398c2ecf20Sopenharmony_ci					    reactivate_supp, extack);
3408c2ecf20Sopenharmony_ci		mlxfw_mfa2_file_component_put(comp);
3418c2ecf20Sopenharmony_ci		if (err)
3428c2ecf20Sopenharmony_ci			return err;
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci	return 0;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ciint mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
3488c2ecf20Sopenharmony_ci			 const struct firmware *firmware,
3498c2ecf20Sopenharmony_ci			 struct netlink_ext_ack *extack)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	struct mlxfw_mfa2_file *mfa2_file;
3528c2ecf20Sopenharmony_ci	bool reactivate_supp = true;
3538c2ecf20Sopenharmony_ci	u32 fwhandle;
3548c2ecf20Sopenharmony_ci	int err;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	if (!mlxfw_mfa2_check(firmware)) {
3578c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack,
3588c2ecf20Sopenharmony_ci			      "Firmware file is not MFA2", -EINVAL);
3598c2ecf20Sopenharmony_ci		return -EINVAL;
3608c2ecf20Sopenharmony_ci	}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	mfa2_file = mlxfw_mfa2_file_init(firmware);
3638c2ecf20Sopenharmony_ci	if (IS_ERR(mfa2_file)) {
3648c2ecf20Sopenharmony_ci		err = PTR_ERR(mfa2_file);
3658c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack,
3668c2ecf20Sopenharmony_ci			      "Failed to initialize MFA2 firmware file", err);
3678c2ecf20Sopenharmony_ci		return err;
3688c2ecf20Sopenharmony_ci	}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	mlxfw_info(mlxfw_dev, "Initialize firmware flash process\n");
3718c2ecf20Sopenharmony_ci	devlink_flash_update_begin_notify(mlxfw_dev->devlink);
3728c2ecf20Sopenharmony_ci	mlxfw_status_notify(mlxfw_dev, "Initializing firmware flash process",
3738c2ecf20Sopenharmony_ci			    NULL, 0, 0);
3748c2ecf20Sopenharmony_ci	err = mlxfw_dev->ops->fsm_lock(mlxfw_dev, &fwhandle);
3758c2ecf20Sopenharmony_ci	if (err) {
3768c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack,
3778c2ecf20Sopenharmony_ci			      "Could not lock the firmware FSM", err);
3788c2ecf20Sopenharmony_ci		goto err_fsm_lock;
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
3828c2ecf20Sopenharmony_ci				   MLXFW_FSM_STATE_LOCKED, extack);
3838c2ecf20Sopenharmony_ci	if (err)
3848c2ecf20Sopenharmony_ci		goto err_state_wait_idle_to_locked;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	err = mlxfw_fsm_reactivate(mlxfw_dev, extack, &reactivate_supp);
3878c2ecf20Sopenharmony_ci	if (err)
3888c2ecf20Sopenharmony_ci		goto err_fsm_reactivate;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
3918c2ecf20Sopenharmony_ci				   MLXFW_FSM_STATE_LOCKED, extack);
3928c2ecf20Sopenharmony_ci	if (err)
3938c2ecf20Sopenharmony_ci		goto err_state_wait_reactivate_to_locked;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	err = mlxfw_flash_components(mlxfw_dev, fwhandle, mfa2_file,
3968c2ecf20Sopenharmony_ci				     reactivate_supp, extack);
3978c2ecf20Sopenharmony_ci	if (err)
3988c2ecf20Sopenharmony_ci		goto err_flash_components;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	mlxfw_dbg(mlxfw_dev, "Activate image\n");
4018c2ecf20Sopenharmony_ci	mlxfw_status_notify(mlxfw_dev, "Activating image", NULL, 0, 0);
4028c2ecf20Sopenharmony_ci	err = mlxfw_dev->ops->fsm_activate(mlxfw_dev, fwhandle);
4038c2ecf20Sopenharmony_ci	if (err) {
4048c2ecf20Sopenharmony_ci		MLXFW_ERR_MSG(mlxfw_dev, extack,
4058c2ecf20Sopenharmony_ci			      "Could not activate the downloaded image", err);
4068c2ecf20Sopenharmony_ci		goto err_fsm_activate;
4078c2ecf20Sopenharmony_ci	}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
4108c2ecf20Sopenharmony_ci				   MLXFW_FSM_STATE_LOCKED, extack);
4118c2ecf20Sopenharmony_ci	if (err)
4128c2ecf20Sopenharmony_ci		goto err_state_wait_activate_to_locked;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	mlxfw_dbg(mlxfw_dev, "Handle release\n");
4158c2ecf20Sopenharmony_ci	mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	mlxfw_info(mlxfw_dev, "Firmware flash done\n");
4188c2ecf20Sopenharmony_ci	mlxfw_status_notify(mlxfw_dev, "Firmware flash done", NULL, 0, 0);
4198c2ecf20Sopenharmony_ci	mlxfw_mfa2_file_fini(mfa2_file);
4208c2ecf20Sopenharmony_ci	devlink_flash_update_end_notify(mlxfw_dev->devlink);
4218c2ecf20Sopenharmony_ci	return 0;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cierr_state_wait_activate_to_locked:
4248c2ecf20Sopenharmony_cierr_fsm_activate:
4258c2ecf20Sopenharmony_cierr_flash_components:
4268c2ecf20Sopenharmony_cierr_state_wait_reactivate_to_locked:
4278c2ecf20Sopenharmony_cierr_fsm_reactivate:
4288c2ecf20Sopenharmony_cierr_state_wait_idle_to_locked:
4298c2ecf20Sopenharmony_ci	mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
4308c2ecf20Sopenharmony_cierr_fsm_lock:
4318c2ecf20Sopenharmony_ci	mlxfw_mfa2_file_fini(mfa2_file);
4328c2ecf20Sopenharmony_ci	devlink_flash_update_end_notify(mlxfw_dev->devlink);
4338c2ecf20Sopenharmony_ci	return err;
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mlxfw_firmware_flash);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
4388c2ecf20Sopenharmony_ciMODULE_AUTHOR("Yotam Gigi <yotamg@mellanox.com>");
4398c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Mellanox firmware flash lib");
440