1 /* SPDX-License-Identifier: Apache-2.0 */ 2 /* Copyright (C) 2018 IBM Corp. */ 3 4 #ifndef MBOX_H 5 #define MBOX_H 6 7 #include <mtd/mtd-abi.h> 8 #include <systemd/sd-bus.h> 9 #include <poll.h> 10 #include <stdbool.h> 11 #include "vpnor/mboxd_pnor_partition_table.h" 12 13 enum api_version { 14 API_VERSION_INVAL = 0, 15 API_VERSION_1 = 1, 16 API_VERSION_2 = 2 17 }; 18 19 #define API_MIN_VERSION API_VERSION_1 20 #define API_MAX_VERSION API_VERSION_2 21 22 #define THIS_NAME "Mailbox Daemon" 23 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 45 #define MBOX_R_SEQ_ERROR 0x08 46 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) 91 enum 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 102 }; 103 104 #define FLASH_OFFSET_UNINIT 0xFFFFFFFF 105 106 struct 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 114 struct window_list { 115 uint32_t num; 116 uint32_t max_age; 117 uint32_t default_size; 118 struct window_context *window; 119 }; 120 121 struct mbox_msg { 122 uint8_t command; 123 uint8_t seq; 124 uint8_t args[MBOX_ARGS_BYTES]; 125 uint8_t response; 126 }; 127 128 union mbox_regs { 129 uint8_t raw[MBOX_REG_BYTES]; 130 struct mbox_msg msg; 131 }; 132 133 struct mbox_context; 134 135 typedef int (*mboxd_mbox_handler)(struct mbox_context *, union mbox_regs *, 136 struct mbox_msg *); 137 138 struct mbox_context { 139 /* System State */ 140 enum mbox_state state; 141 enum api_version version; 142 struct pollfd fds[TOTAL_FDS]; 143 sd_bus *bus; 144 bool terminate; 145 uint8_t bmc_events; 146 uint8_t prev_seq; 147 148 /* Command Dispatch */ 149 const mboxd_mbox_handler *handlers; 150 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; 176 #ifdef VIRTUAL_PNOR_ENABLED 177 /* Virtual PNOR partition table */ 178 struct vpnor_partition_table *vpnor; 179 struct vpnor_partition_paths paths; 180 #endif 181 }; 182 183 #endif /* MBOX_H */ 184