xref: /openbmc/phosphor-mboxd/mbox.h (revision 55dede6b)
1 /*
2  * Copyright 2016 IBM
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef MBOX_H
19 #define MBOX_H
20 
21 #include <mtd/mtd-abi.h>
22 #include <systemd/sd-bus.h>
23 
24 enum api_version {
25 	API_VERSION_INVAL	= 0,
26 	API_VERSION_1		= 1,
27 	API_VERSION_2		= 2
28 };
29 
30 #define API_MIN_VERSION			API_VERSION_1
31 #define API_MAX_VERSION			API_VERSION_2
32 
33 #define THIS_NAME			"Mailbox Daemon"
34 #define SUB_VERSION			0
35 
36 /* Command Values */
37 #define MBOX_C_RESET_STATE		0x01
38 #define MBOX_C_GET_MBOX_INFO		0x02
39 #define MBOX_C_GET_FLASH_INFO		0x03
40 #define MBOX_C_READ_WINDOW		0x04
41 #define MBOX_C_CLOSE_WINDOW		0x05
42 #define MBOX_C_WRITE_WINDOW		0x06
43 #define MBOX_C_WRITE_DIRTY		0x07
44 #define MBOX_C_WRITE_FLUSH		0x08
45 #define MBOX_C_ACK			0x09
46 #define MBOX_C_WRITE_ERASE		0x0a
47 #define NUM_MBOX_CMDS			MBOX_C_WRITE_ERASE
48 
49 /* Response Values */
50 #define MBOX_R_SUCCESS			0x01
51 #define MBOX_R_PARAM_ERROR		0x02
52 #define MBOX_R_WRITE_ERROR		0x03
53 #define MBOX_R_SYSTEM_ERROR		0x04
54 #define MBOX_R_TIMEOUT			0x05
55 #define MBOX_R_BUSY			0x06
56 #define MBOX_R_WINDOW_ERROR		0x07
57 #define MBOX_R_SEQ_ERROR		0x08
58 
59 /* Argument Flags */
60 #define FLAGS_NONE			0x00
61 #define FLAGS_SHORT_LIFETIME		0x01
62 
63 /* BMC Event Notification */
64 #define BMC_EVENT_REBOOT		0x01
65 #define BMC_EVENT_WINDOW_RESET		0x02
66 #define BMC_EVENT_ACK_MASK		(BMC_EVENT_REBOOT | \
67 					BMC_EVENT_WINDOW_RESET)
68 #define BMC_EVENT_FLASH_CTRL_LOST	0x40
69 #define BMC_EVENT_DAEMON_READY		0x80
70 #define BMC_EVENT_V1_MASK		BMC_EVENT_REBOOT
71 #define BMC_EVENT_V2_MASK		(BMC_EVENT_REBOOT | \
72 					BMC_EVENT_WINDOW_RESET | \
73 					BMC_EVENT_FLASH_CTRL_LOST | \
74 					BMC_EVENT_DAEMON_READY)
75 
76 /* MBOX Registers */
77 #define MBOX_HOST_PATH			"/dev/aspeed-mbox"
78 #define MBOX_HOST_TIMEOUT_SEC		1
79 #define MBOX_ARGS_BYTES			11
80 #define MBOX_REG_BYTES			16
81 #define MBOX_HOST_EVENT			14
82 #define MBOX_BMC_EVENT			15
83 
84 #define BLOCK_SIZE_SHIFT_V1		12 /* 4K */
85 
86 /* Window Dirty/Erase bytemap masks */
87 #define WINDOW_CLEAN			0x00
88 #define WINDOW_DIRTY			0x01
89 #define WINDOW_ERASED			0x02
90 
91 /* Put polled file descriptors first */
92 #define DBUS_FD			0
93 #define MBOX_FD			1
94 #define SIG_FD			2
95 #define POLL_FDS		3 /* Number of FDs we poll on */
96 #define LPC_CTRL_FD		3
97 #define MTD_FD			4
98 #define TOTAL_FDS		5
99 
100 #define MAPS_FLASH		(1 << 0)
101 #define MAPS_MEM		(1 << 1)
102 #define STATE_SUSPENDED		(1 << 7)
103 enum mbox_state {
104 	/* Still Initing */
105 	UNINITIALISED = 0,
106 	/* Active and LPC Maps Flash */
107 	ACTIVE_MAPS_FLASH = MAPS_FLASH,
108 	/* Suspended and LPC Maps Flash */
109 	SUSPEND_MAPS_FLASH = STATE_SUSPENDED | MAPS_FLASH,
110 	/* Active and LPC Maps Memory */
111 	ACTIVE_MAPS_MEM = MAPS_MEM,
112 	/* Suspended and LPC Maps Memory */
113 	SUSPEND_MAPS_MEM = STATE_SUSPENDED | MAPS_MEM
114 };
115 
116 #define FLASH_OFFSET_UNINIT	0xFFFFFFFF
117 
118 struct window_context {
119 	void *mem;			/* Portion of Reserved Memory Region */
120 	uint32_t flash_offset;		/* Flash area the window maps (bytes) */
121 	uint32_t size;			/* Window Size (bytes) power-of-2 */
122 	uint8_t *dirty_bmap;		/* Bytemap of the dirty/erased state */
123 	uint32_t age;			/* Used for LRU eviction scheme */
124 };
125 
126 struct window_list {
127 	uint32_t num;
128 	uint32_t max_age;
129 	uint32_t default_size;
130 	struct window_context *window;
131 };
132 
133 struct mbox_context {
134 /* System State */
135 	enum mbox_state state;
136 	enum api_version version;
137 	struct pollfd fds[TOTAL_FDS];
138 	sd_bus *bus;
139 	bool terminate;
140 	uint8_t bmc_events;
141 	uint8_t prev_seq;
142 
143 /* Window State */
144 	/* The window list struct containing all current "windows" */
145 	struct window_list windows;
146 	/* The window the host is currently pointed at */
147 	struct window_context *current;
148 	/* Is the current window a write one */
149 	bool current_is_write;
150 
151 /* Memory & Flash State */
152 	/* Reserved Memory Region */
153 	void *mem;
154 	/* Reserved Mem Size (bytes) */
155 	uint32_t mem_size;
156 	/* LPC Bus Base Address (bytes) */
157 	uint32_t lpc_base;
158 	/* Flash size from command line (bytes) */
159 	uint32_t flash_size;
160 	/* Bytemap of the erased state of the entire flash */
161 	uint8_t *flash_bmap;
162 	/* Erase size (as a shift) */
163 	uint32_t erase_size_shift;
164 	/* Block size (as a shift) */
165 	uint32_t block_size_shift;
166 	/* Actual Flash Info */
167 	struct mtd_info_user mtd_info;
168 };
169 
170 #endif /* MBOX_H */
171