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#ifndef _MLXFW_H
58c2ecf20Sopenharmony_ci#define _MLXFW_H
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/firmware.h>
88c2ecf20Sopenharmony_ci#include <linux/netlink.h>
98c2ecf20Sopenharmony_ci#include <linux/device.h>
108c2ecf20Sopenharmony_ci#include <net/devlink.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistruct mlxfw_dev {
138c2ecf20Sopenharmony_ci	const struct mlxfw_dev_ops *ops;
148c2ecf20Sopenharmony_ci	const char *psid;
158c2ecf20Sopenharmony_ci	u16 psid_size;
168c2ecf20Sopenharmony_ci	struct devlink *devlink;
178c2ecf20Sopenharmony_ci};
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic inline
208c2ecf20Sopenharmony_cistruct device *mlxfw_dev_dev(struct mlxfw_dev *mlxfw_dev)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	return mlxfw_dev->devlink->dev;
238c2ecf20Sopenharmony_ci}
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define MLXFW_PRFX "mlxfw: "
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define mlxfw_info(mlxfw_dev, fmt, ...) \
288c2ecf20Sopenharmony_ci	dev_info(mlxfw_dev_dev(mlxfw_dev), MLXFW_PRFX fmt, ## __VA_ARGS__)
298c2ecf20Sopenharmony_ci#define mlxfw_err(mlxfw_dev, fmt, ...) \
308c2ecf20Sopenharmony_ci	dev_err(mlxfw_dev_dev(mlxfw_dev), MLXFW_PRFX fmt, ## __VA_ARGS__)
318c2ecf20Sopenharmony_ci#define mlxfw_dbg(mlxfw_dev, fmt, ...) \
328c2ecf20Sopenharmony_ci	dev_dbg(mlxfw_dev_dev(mlxfw_dev), MLXFW_PRFX fmt, ## __VA_ARGS__)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cienum mlxfw_fsm_state {
358c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_IDLE,
368c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_LOCKED,
378c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_INITIALIZE,
388c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_DOWNLOAD,
398c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_VERIFY,
408c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_APPLY,
418c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ACTIVATE,
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cienum mlxfw_fsm_state_err {
458c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_OK,
468c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_ERROR,
478c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_REJECTED_DIGEST_ERR,
488c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_REJECTED_NOT_APPLICABLE,
498c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_REJECTED_UNKNOWN_KEY,
508c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_REJECTED_AUTH_FAILED,
518c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_REJECTED_UNSIGNED,
528c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_REJECTED_KEY_NOT_APPLICABLE,
538c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT,
548c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET,
558c2ecf20Sopenharmony_ci	MLXFW_FSM_STATE_ERR_MAX,
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cienum mlxfw_fsm_reactivate_status {
598c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_OK,
608c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_BUSY,
618c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_PROHIBITED_FW_VER_ERR,
628c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_COPY_FAILED,
638c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_ERASE_FAILED,
648c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_RESTORE_FAILED,
658c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_CANDIDATE_FW_DEACTIVATION_FAILED,
668c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_FW_ALREADY_ACTIVATED,
678c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_ERR_DEVICE_RESET_REQUIRED,
688c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_ERR_FW_PROGRAMMING_NEEDED,
698c2ecf20Sopenharmony_ci	MLXFW_FSM_REACTIVATE_STATUS_MAX,
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistruct mlxfw_dev_ops {
738c2ecf20Sopenharmony_ci	int (*component_query)(struct mlxfw_dev *mlxfw_dev, u16 component_index,
748c2ecf20Sopenharmony_ci			       u32 *p_max_size, u8 *p_align_bits,
758c2ecf20Sopenharmony_ci			       u16 *p_max_write_size);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	int (*fsm_lock)(struct mlxfw_dev *mlxfw_dev, u32 *fwhandle);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	int (*fsm_component_update)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
808c2ecf20Sopenharmony_ci				    u16 component_index, u32 component_size);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	int (*fsm_block_download)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
838c2ecf20Sopenharmony_ci				  u8 *data, u16 size, u32 offset);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	int (*fsm_component_verify)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
868c2ecf20Sopenharmony_ci				    u16 component_index);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	int (*fsm_activate)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	int (*fsm_reactivate)(struct mlxfw_dev *mlxfw_dev, u8 *status);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	int (*fsm_query_state)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
938c2ecf20Sopenharmony_ci			       enum mlxfw_fsm_state *fsm_state,
948c2ecf20Sopenharmony_ci			       enum mlxfw_fsm_state_err *fsm_state_err);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	void (*fsm_cancel)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	void (*fsm_release)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle);
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#if IS_REACHABLE(CONFIG_MLXFW)
1028c2ecf20Sopenharmony_ciint mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
1038c2ecf20Sopenharmony_ci			 const struct firmware *firmware,
1048c2ecf20Sopenharmony_ci			 struct netlink_ext_ack *extack);
1058c2ecf20Sopenharmony_ci#else
1068c2ecf20Sopenharmony_cistatic inline
1078c2ecf20Sopenharmony_ciint mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
1088c2ecf20Sopenharmony_ci			 const struct firmware *firmware,
1098c2ecf20Sopenharmony_ci			 struct netlink_ext_ack *extack)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci#endif
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#endif
116