blob: 7ae002b4db638a5ff56f14d97698465965b3c8b4 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301/* SPDX-License-Identifier: Apache-2.0 */
2/* Copyright (C) 2018 IBM Corp. */
Cyril Bur314929b2016-10-14 15:55:16 +11003
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11004#ifndef MBOX_H
5#define MBOX_H
Cyril Bur314929b2016-10-14 15:55:16 +11006
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11007#include <mtd/mtd-abi.h>
8#include <systemd/sd-bus.h>
Andrew Jeffery8ecbdb52017-04-10 16:26:08 +09309#include <poll.h>
10#include <stdbool.h>
Andrew Jeffery53c21aa2018-03-26 11:56:16 +103011#include "vpnor/mboxd_pnor_partition_table.h"
Cyril Bur314929b2016-10-14 15:55:16 +110012
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110013enum api_version {
14 API_VERSION_INVAL = 0,
15 API_VERSION_1 = 1,
16 API_VERSION_2 = 2
Cyril Bur314929b2016-10-14 15:55:16 +110017};
18
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110019#define API_MIN_VERSION API_VERSION_1
20#define API_MAX_VERSION API_VERSION_2
21
22#define THIS_NAME "Mailbox Daemon"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110023
24/* Command Values */
25#define MBOX_C_RESET_STATE 0x01
26#define MBOX_C_GET_MBOX_INFO 0x02
27#define MBOX_C_GET_FLASH_INFO 0x03
28#define MBOX_C_READ_WINDOW 0x04
29#define MBOX_C_CLOSE_WINDOW 0x05
30#define MBOX_C_WRITE_WINDOW 0x06
31#define MBOX_C_WRITE_DIRTY 0x07
32#define MBOX_C_WRITE_FLUSH 0x08
33#define MBOX_C_ACK 0x09
34#define MBOX_C_WRITE_ERASE 0x0a
35#define NUM_MBOX_CMDS MBOX_C_WRITE_ERASE
36
37/* Response Values */
38#define MBOX_R_SUCCESS 0x01
39#define MBOX_R_PARAM_ERROR 0x02
40#define MBOX_R_WRITE_ERROR 0x03
41#define MBOX_R_SYSTEM_ERROR 0x04
42#define MBOX_R_TIMEOUT 0x05
43#define MBOX_R_BUSY 0x06
44#define MBOX_R_WINDOW_ERROR 0x07
Andrew Jeffery55dede62017-04-24 16:13:06 +093045#define MBOX_R_SEQ_ERROR 0x08
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110046
47/* Argument Flags */
48#define FLAGS_NONE 0x00
49#define FLAGS_SHORT_LIFETIME 0x01
50
51/* BMC Event Notification */
52#define BMC_EVENT_REBOOT 0x01
53#define BMC_EVENT_WINDOW_RESET 0x02
54#define BMC_EVENT_ACK_MASK (BMC_EVENT_REBOOT | \
55 BMC_EVENT_WINDOW_RESET)
56#define BMC_EVENT_FLASH_CTRL_LOST 0x40
57#define BMC_EVENT_DAEMON_READY 0x80
58#define BMC_EVENT_V1_MASK BMC_EVENT_REBOOT
59#define BMC_EVENT_V2_MASK (BMC_EVENT_REBOOT | \
60 BMC_EVENT_WINDOW_RESET | \
61 BMC_EVENT_FLASH_CTRL_LOST | \
62 BMC_EVENT_DAEMON_READY)
63
64/* MBOX Registers */
65#define MBOX_HOST_PATH "/dev/aspeed-mbox"
66#define MBOX_HOST_TIMEOUT_SEC 1
67#define MBOX_ARGS_BYTES 11
68#define MBOX_REG_BYTES 16
69#define MBOX_HOST_EVENT 14
70#define MBOX_BMC_EVENT 15
71
72#define BLOCK_SIZE_SHIFT_V1 12 /* 4K */
73
74/* Window Dirty/Erase bytemap masks */
75#define WINDOW_CLEAN 0x00
76#define WINDOW_DIRTY 0x01
77#define WINDOW_ERASED 0x02
78
79/* Put polled file descriptors first */
80#define DBUS_FD 0
81#define MBOX_FD 1
82#define SIG_FD 2
83#define POLL_FDS 3 /* Number of FDs we poll on */
84#define LPC_CTRL_FD 3
85#define MTD_FD 4
86#define TOTAL_FDS 5
87
88#define MAPS_FLASH (1 << 0)
89#define MAPS_MEM (1 << 1)
90#define STATE_SUSPENDED (1 << 7)
91enum mbox_state {
92 /* Still Initing */
93 UNINITIALISED = 0,
94 /* Active and LPC Maps Flash */
95 ACTIVE_MAPS_FLASH = MAPS_FLASH,
96 /* Suspended and LPC Maps Flash */
97 SUSPEND_MAPS_FLASH = STATE_SUSPENDED | MAPS_FLASH,
98 /* Active and LPC Maps Memory */
99 ACTIVE_MAPS_MEM = MAPS_MEM,
100 /* Suspended and LPC Maps Memory */
101 SUSPEND_MAPS_MEM = STATE_SUSPENDED | MAPS_MEM
Cyril Bur314929b2016-10-14 15:55:16 +1100102};
103
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100104#define FLASH_OFFSET_UNINIT 0xFFFFFFFF
Cyril Bur314929b2016-10-14 15:55:16 +1100105
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100106struct window_context {
107 void *mem; /* Portion of Reserved Memory Region */
108 uint32_t flash_offset; /* Flash area the window maps (bytes) */
109 uint32_t size; /* Window Size (bytes) power-of-2 */
110 uint8_t *dirty_bmap; /* Bytemap of the dirty/erased state */
111 uint32_t age; /* Used for LRU eviction scheme */
112};
113
114struct window_list {
115 uint32_t num;
116 uint32_t max_age;
117 uint32_t default_size;
118 struct window_context *window;
119};
120
Andrew Jefferyefb09de2018-03-26 14:36:43 +1030121struct mbox_msg {
122 uint8_t command;
123 uint8_t seq;
124 uint8_t args[MBOX_ARGS_BYTES];
125 uint8_t response;
126};
127
128union mbox_regs {
129 uint8_t raw[MBOX_REG_BYTES];
130 struct mbox_msg msg;
131};
132
133typedef int (*mboxd_mbox_handler)(struct mbox_context *, union mbox_regs *,
134 struct mbox_msg *);
135
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100136struct mbox_context {
137/* System State */
138 enum mbox_state state;
139 enum api_version version;
140 struct pollfd fds[TOTAL_FDS];
141 sd_bus *bus;
142 bool terminate;
143 uint8_t bmc_events;
Andrew Jeffery55dede62017-04-24 16:13:06 +0930144 uint8_t prev_seq;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100145
Andrew Jefferyefb09de2018-03-26 14:36:43 +1030146/* Command Dispatch */
147 const mboxd_mbox_handler *handlers;
148
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100149/* Window State */
150 /* The window list struct containing all current "windows" */
151 struct window_list windows;
152 /* The window the host is currently pointed at */
153 struct window_context *current;
154 /* Is the current window a write one */
155 bool current_is_write;
156
157/* Memory & Flash State */
158 /* Reserved Memory Region */
159 void *mem;
160 /* Reserved Mem Size (bytes) */
161 uint32_t mem_size;
162 /* LPC Bus Base Address (bytes) */
163 uint32_t lpc_base;
164 /* Flash size from command line (bytes) */
165 uint32_t flash_size;
166 /* Bytemap of the erased state of the entire flash */
167 uint8_t *flash_bmap;
168 /* Erase size (as a shift) */
169 uint32_t erase_size_shift;
170 /* Block size (as a shift) */
171 uint32_t block_size_shift;
172 /* Actual Flash Info */
173 struct mtd_info_user mtd_info;
Deepak Kodihallib6a446f2017-04-29 13:01:49 -0500174#ifdef VIRTUAL_PNOR_ENABLED
175 /* Virtual PNOR partition table */
176 struct vpnor_partition_table *vpnor;
Ratan Gupta8441a392017-05-05 21:42:53 +0530177 struct vpnor_partition_paths paths;
Deepak Kodihallib6a446f2017-04-29 13:01:49 -0500178#endif
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100179};
180
181#endif /* MBOX_H */