xref: /openbmc/phosphor-mboxd/mbox.h (revision 5b7b018ce2e472280f980b543fa835396bf0bd5c)
14fe996c2SAndrew Jeffery /* SPDX-License-Identifier: Apache-2.0 */
24fe996c2SAndrew Jeffery /* Copyright (C) 2018 IBM Corp. */
3314929b4SCyril Bur 
4e39c9163SSuraj Jitindar Singh #ifndef MBOX_H
5e39c9163SSuraj Jitindar Singh #define MBOX_H
6e39c9163SSuraj Jitindar Singh 
7e39c9163SSuraj Jitindar Singh #include <mtd/mtd-abi.h>
8e39c9163SSuraj Jitindar Singh #include <systemd/sd-bus.h>
98ecbdb54SAndrew Jeffery #include <poll.h>
108ecbdb54SAndrew Jeffery #include <stdbool.h>
1153c21aaaSAndrew Jeffery #include "vpnor/mboxd_pnor_partition_table.h"
12e39c9163SSuraj Jitindar Singh 
13e39c9163SSuraj Jitindar Singh enum api_version {
14e39c9163SSuraj Jitindar Singh 	API_VERSION_INVAL	= 0,
15e39c9163SSuraj Jitindar Singh 	API_VERSION_1		= 1,
16e39c9163SSuraj Jitindar Singh 	API_VERSION_2		= 2
17e39c9163SSuraj Jitindar Singh };
18e39c9163SSuraj Jitindar Singh 
19e39c9163SSuraj Jitindar Singh #define API_MIN_VERSION			API_VERSION_1
20e39c9163SSuraj Jitindar Singh #define API_MAX_VERSION			API_VERSION_2
21e39c9163SSuraj Jitindar Singh 
22e39c9163SSuraj Jitindar Singh #define THIS_NAME			"Mailbox Daemon"
23e39c9163SSuraj Jitindar Singh 
24e39c9163SSuraj Jitindar Singh /* Command Values */
25314929b4SCyril Bur #define MBOX_C_RESET_STATE		0x01
26314929b4SCyril Bur #define MBOX_C_GET_MBOX_INFO		0x02
27314929b4SCyril Bur #define MBOX_C_GET_FLASH_INFO		0x03
28314929b4SCyril Bur #define MBOX_C_READ_WINDOW		0x04
29314929b4SCyril Bur #define MBOX_C_CLOSE_WINDOW		0x05
30314929b4SCyril Bur #define MBOX_C_WRITE_WINDOW		0x06
31314929b4SCyril Bur #define MBOX_C_WRITE_DIRTY		0x07
32e39c9163SSuraj Jitindar Singh #define MBOX_C_WRITE_FLUSH		0x08
33314929b4SCyril Bur #define MBOX_C_ACK			0x09
34e39c9163SSuraj Jitindar Singh #define MBOX_C_WRITE_ERASE		0x0a
35e39c9163SSuraj Jitindar Singh #define NUM_MBOX_CMDS			MBOX_C_WRITE_ERASE
36314929b4SCyril Bur 
37e39c9163SSuraj Jitindar Singh /* Response Values */
38314929b4SCyril Bur #define MBOX_R_SUCCESS			0x01
39314929b4SCyril Bur #define MBOX_R_PARAM_ERROR		0x02
40314929b4SCyril Bur #define MBOX_R_WRITE_ERROR		0x03
41e39c9163SSuraj Jitindar Singh #define MBOX_R_SYSTEM_ERROR		0x04
42314929b4SCyril Bur #define MBOX_R_TIMEOUT			0x05
43e39c9163SSuraj Jitindar Singh #define MBOX_R_BUSY			0x06
44e39c9163SSuraj Jitindar Singh #define MBOX_R_WINDOW_ERROR		0x07
4555dede6bSAndrew Jeffery #define MBOX_R_SEQ_ERROR		0x08
46314929b4SCyril Bur 
47e39c9163SSuraj Jitindar Singh /* Argument Flags */
48e39c9163SSuraj Jitindar Singh #define FLAGS_NONE			0x00
49e39c9163SSuraj Jitindar Singh #define FLAGS_SHORT_LIFETIME		0x01
50e39c9163SSuraj Jitindar Singh 
51e39c9163SSuraj Jitindar Singh /* BMC Event Notification */
52e39c9163SSuraj Jitindar Singh #define BMC_EVENT_REBOOT		0x01
53e39c9163SSuraj Jitindar Singh #define BMC_EVENT_WINDOW_RESET		0x02
54e39c9163SSuraj Jitindar Singh #define BMC_EVENT_ACK_MASK		(BMC_EVENT_REBOOT | \
55e39c9163SSuraj Jitindar Singh 					BMC_EVENT_WINDOW_RESET)
56e39c9163SSuraj Jitindar Singh #define BMC_EVENT_FLASH_CTRL_LOST	0x40
57e39c9163SSuraj Jitindar Singh #define BMC_EVENT_DAEMON_READY		0x80
58e39c9163SSuraj Jitindar Singh #define BMC_EVENT_V1_MASK		BMC_EVENT_REBOOT
59e39c9163SSuraj Jitindar Singh #define BMC_EVENT_V2_MASK		(BMC_EVENT_REBOOT | \
60e39c9163SSuraj Jitindar Singh 					BMC_EVENT_WINDOW_RESET | \
61e39c9163SSuraj Jitindar Singh 					BMC_EVENT_FLASH_CTRL_LOST | \
62e39c9163SSuraj Jitindar Singh 					BMC_EVENT_DAEMON_READY)
63e39c9163SSuraj Jitindar Singh 
64e39c9163SSuraj Jitindar Singh /* MBOX Registers */
65314929b4SCyril Bur #define MBOX_HOST_PATH			"/dev/aspeed-mbox"
66314929b4SCyril Bur #define MBOX_HOST_TIMEOUT_SEC		1
67e39c9163SSuraj Jitindar Singh #define MBOX_ARGS_BYTES			11
68314929b4SCyril Bur #define MBOX_REG_BYTES			16
69e39c9163SSuraj Jitindar Singh #define MBOX_HOST_EVENT			14
70e39c9163SSuraj Jitindar Singh #define MBOX_BMC_EVENT			15
71314929b4SCyril Bur 
72e39c9163SSuraj Jitindar Singh #define BLOCK_SIZE_SHIFT_V1		12 /* 4K */
73e39c9163SSuraj Jitindar Singh 
74e39c9163SSuraj Jitindar Singh /* Window Dirty/Erase bytemap masks */
75e39c9163SSuraj Jitindar Singh #define WINDOW_CLEAN			0x00
76e39c9163SSuraj Jitindar Singh #define WINDOW_DIRTY			0x01
77e39c9163SSuraj Jitindar Singh #define WINDOW_ERASED			0x02
78e39c9163SSuraj Jitindar Singh 
79e39c9163SSuraj Jitindar Singh /* Put polled file descriptors first */
80e39c9163SSuraj Jitindar Singh #define DBUS_FD			0
81e39c9163SSuraj Jitindar Singh #define MBOX_FD			1
82e39c9163SSuraj Jitindar Singh #define SIG_FD			2
83e39c9163SSuraj Jitindar Singh #define POLL_FDS		3 /* Number of FDs we poll on */
84e39c9163SSuraj Jitindar Singh #define LPC_CTRL_FD		3
85e39c9163SSuraj Jitindar Singh #define MTD_FD			4
86e39c9163SSuraj Jitindar Singh #define TOTAL_FDS		5
87e39c9163SSuraj Jitindar Singh 
88e39c9163SSuraj Jitindar Singh #define MAPS_FLASH		(1 << 0)
89e39c9163SSuraj Jitindar Singh #define MAPS_MEM		(1 << 1)
90e39c9163SSuraj Jitindar Singh #define STATE_SUSPENDED		(1 << 7)
91e39c9163SSuraj Jitindar Singh enum mbox_state {
92e39c9163SSuraj Jitindar Singh 	/* Still Initing */
93e39c9163SSuraj Jitindar Singh 	UNINITIALISED = 0,
94e39c9163SSuraj Jitindar Singh 	/* Active and LPC Maps Flash */
95e39c9163SSuraj Jitindar Singh 	ACTIVE_MAPS_FLASH = MAPS_FLASH,
96e39c9163SSuraj Jitindar Singh 	/* Suspended and LPC Maps Flash */
97e39c9163SSuraj Jitindar Singh 	SUSPEND_MAPS_FLASH = STATE_SUSPENDED | MAPS_FLASH,
98e39c9163SSuraj Jitindar Singh 	/* Active and LPC Maps Memory */
99e39c9163SSuraj Jitindar Singh 	ACTIVE_MAPS_MEM = MAPS_MEM,
100e39c9163SSuraj Jitindar Singh 	/* Suspended and LPC Maps Memory */
101e39c9163SSuraj Jitindar Singh 	SUSPEND_MAPS_MEM = STATE_SUSPENDED | MAPS_MEM
102314929b4SCyril Bur };
103314929b4SCyril Bur 
104e39c9163SSuraj Jitindar Singh #define FLASH_OFFSET_UNINIT	0xFFFFFFFF
105e39c9163SSuraj Jitindar Singh 
106e39c9163SSuraj Jitindar Singh struct window_context {
107e39c9163SSuraj Jitindar Singh 	void *mem;			/* Portion of Reserved Memory Region */
108e39c9163SSuraj Jitindar Singh 	uint32_t flash_offset;		/* Flash area the window maps (bytes) */
109e39c9163SSuraj Jitindar Singh 	uint32_t size;			/* Window Size (bytes) power-of-2 */
110e39c9163SSuraj Jitindar Singh 	uint8_t *dirty_bmap;		/* Bytemap of the dirty/erased state */
111e39c9163SSuraj Jitindar Singh 	uint32_t age;			/* Used for LRU eviction scheme */
112314929b4SCyril Bur };
113314929b4SCyril Bur 
114e39c9163SSuraj Jitindar Singh struct window_list {
115e39c9163SSuraj Jitindar Singh 	uint32_t num;
116e39c9163SSuraj Jitindar Singh 	uint32_t max_age;
117e39c9163SSuraj Jitindar Singh 	uint32_t default_size;
118e39c9163SSuraj Jitindar Singh 	struct window_context *window;
119e39c9163SSuraj Jitindar Singh };
120314929b4SCyril Bur 
121efb09defSAndrew Jeffery struct mbox_msg {
122efb09defSAndrew Jeffery 	uint8_t command;
123efb09defSAndrew Jeffery 	uint8_t seq;
124efb09defSAndrew Jeffery 	uint8_t args[MBOX_ARGS_BYTES];
125efb09defSAndrew Jeffery 	uint8_t response;
126efb09defSAndrew Jeffery };
127efb09defSAndrew Jeffery 
128efb09defSAndrew Jeffery union mbox_regs {
129efb09defSAndrew Jeffery 	uint8_t raw[MBOX_REG_BYTES];
130efb09defSAndrew Jeffery 	struct mbox_msg msg;
131efb09defSAndrew Jeffery };
132efb09defSAndrew Jeffery 
133*5b7b018cSAndrew Jeffery struct mbox_context;
134*5b7b018cSAndrew Jeffery 
135efb09defSAndrew Jeffery typedef int (*mboxd_mbox_handler)(struct mbox_context *, union mbox_regs *,
136efb09defSAndrew Jeffery 				  struct mbox_msg *);
137efb09defSAndrew Jeffery 
138e39c9163SSuraj Jitindar Singh struct mbox_context {
139e39c9163SSuraj Jitindar Singh /* System State */
140e39c9163SSuraj Jitindar Singh 	enum mbox_state state;
141e39c9163SSuraj Jitindar Singh 	enum api_version version;
142e39c9163SSuraj Jitindar Singh 	struct pollfd fds[TOTAL_FDS];
143e39c9163SSuraj Jitindar Singh 	sd_bus *bus;
144e39c9163SSuraj Jitindar Singh 	bool terminate;
145e39c9163SSuraj Jitindar Singh 	uint8_t bmc_events;
14655dede6bSAndrew Jeffery 	uint8_t prev_seq;
147e39c9163SSuraj Jitindar Singh 
148efb09defSAndrew Jeffery /* Command Dispatch */
149efb09defSAndrew Jeffery 	const mboxd_mbox_handler *handlers;
150efb09defSAndrew Jeffery 
151e39c9163SSuraj Jitindar Singh /* Window State */
152e39c9163SSuraj Jitindar Singh 	/* The window list struct containing all current "windows" */
153e39c9163SSuraj Jitindar Singh 	struct window_list windows;
154e39c9163SSuraj Jitindar Singh 	/* The window the host is currently pointed at */
155e39c9163SSuraj Jitindar Singh 	struct window_context *current;
156e39c9163SSuraj Jitindar Singh 	/* Is the current window a write one */
157e39c9163SSuraj Jitindar Singh 	bool current_is_write;
158e39c9163SSuraj Jitindar Singh 
159e39c9163SSuraj Jitindar Singh /* Memory & Flash State */
160e39c9163SSuraj Jitindar Singh 	/* Reserved Memory Region */
161e39c9163SSuraj Jitindar Singh 	void *mem;
162e39c9163SSuraj Jitindar Singh 	/* Reserved Mem Size (bytes) */
163e39c9163SSuraj Jitindar Singh 	uint32_t mem_size;
164e39c9163SSuraj Jitindar Singh 	/* LPC Bus Base Address (bytes) */
165e39c9163SSuraj Jitindar Singh 	uint32_t lpc_base;
166e39c9163SSuraj Jitindar Singh 	/* Flash size from command line (bytes) */
167e39c9163SSuraj Jitindar Singh 	uint32_t flash_size;
168e39c9163SSuraj Jitindar Singh 	/* Bytemap of the erased state of the entire flash */
169e39c9163SSuraj Jitindar Singh 	uint8_t *flash_bmap;
170e39c9163SSuraj Jitindar Singh 	/* Erase size (as a shift) */
171e39c9163SSuraj Jitindar Singh 	uint32_t erase_size_shift;
172e39c9163SSuraj Jitindar Singh 	/* Block size (as a shift) */
173e39c9163SSuraj Jitindar Singh 	uint32_t block_size_shift;
174e39c9163SSuraj Jitindar Singh 	/* Actual Flash Info */
175e39c9163SSuraj Jitindar Singh 	struct mtd_info_user mtd_info;
176b6a446f9SDeepak Kodihalli #ifdef VIRTUAL_PNOR_ENABLED
177b6a446f9SDeepak Kodihalli 	/* Virtual PNOR partition table */
178b6a446f9SDeepak Kodihalli 	struct vpnor_partition_table *vpnor;
1798441a395SRatan Gupta 	struct vpnor_partition_paths paths;
180b6a446f9SDeepak Kodihalli #endif
181e39c9163SSuraj Jitindar Singh };
182e39c9163SSuraj Jitindar Singh 
183e39c9163SSuraj Jitindar Singh #endif /* MBOX_H */
184