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