blob: 3676825ebd286328546bfac9c46430f9a11a057b [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 Jeffery22fa5002018-08-07 15:22:50 +0930136
Andrew Jefferyefb09de2018-03-26 14:36:43 +1030137typedef int (*mboxd_mbox_handler)(struct mbox_context *, union mbox_regs *,
138 struct mbox_msg *);
139
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100140struct mbox_context {
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930141 enum api_version version;
142 const struct protocol_ops *protocol;
143
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100144/* System State */
145 enum mbox_state state;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100146 struct pollfd fds[TOTAL_FDS];
147 sd_bus *bus;
148 bool terminate;
149 uint8_t bmc_events;
Andrew Jeffery55dede62017-04-24 16:13:06 +0930150 uint8_t prev_seq;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100151
Andrew Jefferyefb09de2018-03-26 14:36:43 +1030152/* Command Dispatch */
153 const mboxd_mbox_handler *handlers;
154
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100155/* Window State */
156 /* The window list struct containing all current "windows" */
157 struct window_list windows;
158 /* The window the host is currently pointed at */
159 struct window_context *current;
160 /* Is the current window a write one */
161 bool current_is_write;
162
163/* Memory & Flash State */
164 /* Reserved Memory Region */
165 void *mem;
166 /* Reserved Mem Size (bytes) */
167 uint32_t mem_size;
168 /* LPC Bus Base Address (bytes) */
169 uint32_t lpc_base;
170 /* Flash size from command line (bytes) */
171 uint32_t flash_size;
172 /* Bytemap of the erased state of the entire flash */
173 uint8_t *flash_bmap;
174 /* Erase size (as a shift) */
175 uint32_t erase_size_shift;
176 /* Block size (as a shift) */
177 uint32_t block_size_shift;
178 /* Actual Flash Info */
179 struct mtd_info_user mtd_info;
Deepak Kodihallib6a446f2017-04-29 13:01:49 -0500180#ifdef VIRTUAL_PNOR_ENABLED
181 /* Virtual PNOR partition table */
182 struct vpnor_partition_table *vpnor;
Ratan Gupta8441a392017-05-05 21:42:53 +0530183 struct vpnor_partition_paths paths;
Deepak Kodihallib6a446f2017-04-29 13:01:49 -0500184#endif
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100185};
186
187#endif /* MBOX_H */