1 /*
2  * drivers/net/ethernet/mellanox/mlxfw/mlxfw.h
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 #ifndef _MLXFW_H
36 #define _MLXFW_H
37 
38 #include <linux/firmware.h>
39 
40 enum mlxfw_fsm_state {
41 	MLXFW_FSM_STATE_IDLE,
42 	MLXFW_FSM_STATE_LOCKED,
43 	MLXFW_FSM_STATE_INITIALIZE,
44 	MLXFW_FSM_STATE_DOWNLOAD,
45 	MLXFW_FSM_STATE_VERIFY,
46 	MLXFW_FSM_STATE_APPLY,
47 	MLXFW_FSM_STATE_ACTIVATE,
48 };
49 
50 enum mlxfw_fsm_state_err {
51 	MLXFW_FSM_STATE_ERR_OK,
52 	MLXFW_FSM_STATE_ERR_ERROR,
53 	MLXFW_FSM_STATE_ERR_REJECTED_DIGEST_ERR,
54 	MLXFW_FSM_STATE_ERR_REJECTED_NOT_APPLICABLE,
55 	MLXFW_FSM_STATE_ERR_REJECTED_UNKNOWN_KEY,
56 	MLXFW_FSM_STATE_ERR_REJECTED_AUTH_FAILED,
57 	MLXFW_FSM_STATE_ERR_REJECTED_UNSIGNED,
58 	MLXFW_FSM_STATE_ERR_REJECTED_KEY_NOT_APPLICABLE,
59 	MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT,
60 	MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET,
61 	MLXFW_FSM_STATE_ERR_MAX,
62 };
63 
64 struct mlxfw_dev;
65 
66 struct mlxfw_dev_ops {
67 	int (*component_query)(struct mlxfw_dev *mlxfw_dev, u16 component_index,
68 			       u32 *p_max_size, u8 *p_align_bits,
69 			       u16 *p_max_write_size);
70 
71 	int (*fsm_lock)(struct mlxfw_dev *mlxfw_dev, u32 *fwhandle);
72 
73 	int (*fsm_component_update)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
74 				    u16 component_index, u32 component_size);
75 
76 	int (*fsm_block_download)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
77 				  u8 *data, u16 size, u32 offset);
78 
79 	int (*fsm_component_verify)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
80 				    u16 component_index);
81 
82 	int (*fsm_activate)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle);
83 
84 	int (*fsm_query_state)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
85 			       enum mlxfw_fsm_state *fsm_state,
86 			       enum mlxfw_fsm_state_err *fsm_state_err);
87 
88 	void (*fsm_cancel)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle);
89 
90 	void (*fsm_release)(struct mlxfw_dev *mlxfw_dev, u32 fwhandle);
91 };
92 
93 struct mlxfw_dev {
94 	const struct mlxfw_dev_ops *ops;
95 	const char *psid;
96 	u16 psid_size;
97 };
98 
99 #if IS_REACHABLE(CONFIG_MLXFW)
100 int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
101 			 const struct firmware *firmware);
102 #else
103 static inline
104 int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
105 			 const struct firmware *firmware)
106 {
107 	return -EOPNOTSUPP;
108 }
109 #endif
110 
111 #endif
112