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