xref: /openbmc/phosphor-mboxd/mbox.h (revision 04e8ffda)
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 
58 /* Argument Flags */
59 #define FLAGS_NONE			0x00
60 #define FLAGS_SHORT_LIFETIME		0x01
61 
62 /* BMC Event Notification */
63 #define BMC_EVENT_REBOOT		0x01
64 #define BMC_EVENT_WINDOW_RESET		0x02
65 #define BMC_EVENT_ACK_MASK		(BMC_EVENT_REBOOT | \
66 					BMC_EVENT_WINDOW_RESET)
67 #define BMC_EVENT_FLASH_CTRL_LOST	0x40
68 #define BMC_EVENT_DAEMON_READY		0x80
69 #define BMC_EVENT_V1_MASK		BMC_EVENT_REBOOT
70 #define BMC_EVENT_V2_MASK		(BMC_EVENT_REBOOT | \
71 					BMC_EVENT_WINDOW_RESET | \
72 					BMC_EVENT_FLASH_CTRL_LOST | \
73 					BMC_EVENT_DAEMON_READY)
74 
75 /* MBOX Registers */
76 #define MBOX_HOST_PATH			"/dev/aspeed-mbox"
77 #define MBOX_HOST_TIMEOUT_SEC		1
78 #define MBOX_ARGS_BYTES			11
79 #define MBOX_REG_BYTES			16
80 #define MBOX_HOST_EVENT			14
81 #define MBOX_BMC_EVENT			15
82 
83 #define BLOCK_SIZE_SHIFT_V1		12 /* 4K */
84 
85 /* Window Dirty/Erase bytemap masks */
86 #define WINDOW_CLEAN			0x00
87 #define WINDOW_DIRTY			0x01
88 #define WINDOW_ERASED			0x02
89 
90 /* Put polled file descriptors first */
91 #define DBUS_FD			0
92 #define MBOX_FD			1
93 #define SIG_FD			2
94 #define POLL_FDS		3 /* Number of FDs we poll on */
95 #define LPC_CTRL_FD		3
96 #define MTD_FD			4
97 #define TOTAL_FDS		5
98 
99 #define MAPS_FLASH		(1 << 0)
100 #define MAPS_MEM		(1 << 1)
101 #define STATE_SUSPENDED		(1 << 7)
102 enum mbox_state {
103 	/* Still Initing */
104 	UNINITIALISED = 0,
105 	/* Active and LPC Maps Flash */
106 	ACTIVE_MAPS_FLASH = MAPS_FLASH,
107 	/* Suspended and LPC Maps Flash */
108 	SUSPEND_MAPS_FLASH = STATE_SUSPENDED | MAPS_FLASH,
109 	/* Active and LPC Maps Memory */
110 	ACTIVE_MAPS_MEM = MAPS_MEM,
111 	/* Suspended and LPC Maps Memory */
112 	SUSPEND_MAPS_MEM = STATE_SUSPENDED | MAPS_MEM
113 };
114 
115 #define FLASH_OFFSET_UNINIT	0xFFFFFFFF
116 
117 struct window_context {
118 	void *mem;			/* Portion of Reserved Memory Region */
119 	uint32_t flash_offset;		/* Flash area the window maps (bytes) */
120 	uint32_t size;			/* Window Size (bytes) power-of-2 */
121 	uint8_t *dirty_bmap;		/* Bytemap of the dirty/erased state */
122 	uint32_t age;			/* Used for LRU eviction scheme */
123 };
124 
125 struct window_list {
126 	uint32_t num;
127 	uint32_t max_age;
128 	uint32_t default_size;
129 	struct window_context *window;
130 };
131 
132 struct mbox_context {
133 /* System State */
134 	enum mbox_state state;
135 	enum api_version version;
136 	struct pollfd fds[TOTAL_FDS];
137 	sd_bus *bus;
138 	bool terminate;
139 	uint8_t bmc_events;
140 
141 /* Window State */
142 	/* The window list struct containing all current "windows" */
143 	struct window_list windows;
144 	/* The window the host is currently pointed at */
145 	struct window_context *current;
146 	/* Is the current window a write one */
147 	bool current_is_write;
148 
149 /* Memory & Flash State */
150 	/* Reserved Memory Region */
151 	void *mem;
152 	/* Reserved Mem Size (bytes) */
153 	uint32_t mem_size;
154 	/* LPC Bus Base Address (bytes) */
155 	uint32_t lpc_base;
156 	/* Flash size from command line (bytes) */
157 	uint32_t flash_size;
158 	/* Bytemap of the erased state of the entire flash */
159 	uint8_t *flash_bmap;
160 	/* Erase size (as a shift) */
161 	uint32_t erase_size_shift;
162 	/* Block size (as a shift) */
163 	uint32_t block_size_shift;
164 	/* Actual Flash Info */
165 	struct mtd_info_user mtd_info;
166 };
167 
168 #endif /* MBOX_H */
169