1 /* 2 * Copyright 2016 IBM 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 #ifndef MBOX_H 19 #define MBOX_H 20 21 #include <mtd/mtd-abi.h> 22 #include <systemd/sd-bus.h> 23 #include <poll.h> 24 #include <stdbool.h> 25 26 enum api_version { 27 API_VERSION_INVAL = 0, 28 API_VERSION_1 = 1, 29 API_VERSION_2 = 2 30 }; 31 32 #define API_MIN_VERSION API_VERSION_1 33 #define API_MAX_VERSION API_VERSION_2 34 35 #define THIS_NAME "Mailbox Daemon" 36 37 /* Command Values */ 38 #define MBOX_C_RESET_STATE 0x01 39 #define MBOX_C_GET_MBOX_INFO 0x02 40 #define MBOX_C_GET_FLASH_INFO 0x03 41 #define MBOX_C_READ_WINDOW 0x04 42 #define MBOX_C_CLOSE_WINDOW 0x05 43 #define MBOX_C_WRITE_WINDOW 0x06 44 #define MBOX_C_WRITE_DIRTY 0x07 45 #define MBOX_C_WRITE_FLUSH 0x08 46 #define MBOX_C_ACK 0x09 47 #define MBOX_C_WRITE_ERASE 0x0a 48 #define NUM_MBOX_CMDS MBOX_C_WRITE_ERASE 49 50 /* Response Values */ 51 #define MBOX_R_SUCCESS 0x01 52 #define MBOX_R_PARAM_ERROR 0x02 53 #define MBOX_R_WRITE_ERROR 0x03 54 #define MBOX_R_SYSTEM_ERROR 0x04 55 #define MBOX_R_TIMEOUT 0x05 56 #define MBOX_R_BUSY 0x06 57 #define MBOX_R_WINDOW_ERROR 0x07 58 #define MBOX_R_SEQ_ERROR 0x08 59 60 /* Argument Flags */ 61 #define FLAGS_NONE 0x00 62 #define FLAGS_SHORT_LIFETIME 0x01 63 64 /* BMC Event Notification */ 65 #define BMC_EVENT_REBOOT 0x01 66 #define BMC_EVENT_WINDOW_RESET 0x02 67 #define BMC_EVENT_ACK_MASK (BMC_EVENT_REBOOT | \ 68 BMC_EVENT_WINDOW_RESET) 69 #define BMC_EVENT_FLASH_CTRL_LOST 0x40 70 #define BMC_EVENT_DAEMON_READY 0x80 71 #define BMC_EVENT_V1_MASK BMC_EVENT_REBOOT 72 #define BMC_EVENT_V2_MASK (BMC_EVENT_REBOOT | \ 73 BMC_EVENT_WINDOW_RESET | \ 74 BMC_EVENT_FLASH_CTRL_LOST | \ 75 BMC_EVENT_DAEMON_READY) 76 77 /* MBOX Registers */ 78 #define MBOX_HOST_PATH "/dev/aspeed-mbox" 79 #define MBOX_HOST_TIMEOUT_SEC 1 80 #define MBOX_ARGS_BYTES 11 81 #define MBOX_REG_BYTES 16 82 #define MBOX_HOST_EVENT 14 83 #define MBOX_BMC_EVENT 15 84 85 #define BLOCK_SIZE_SHIFT_V1 12 /* 4K */ 86 87 /* Window Dirty/Erase bytemap masks */ 88 #define WINDOW_CLEAN 0x00 89 #define WINDOW_DIRTY 0x01 90 #define WINDOW_ERASED 0x02 91 92 /* Put polled file descriptors first */ 93 #define DBUS_FD 0 94 #define MBOX_FD 1 95 #define SIG_FD 2 96 #define POLL_FDS 3 /* Number of FDs we poll on */ 97 #define LPC_CTRL_FD 3 98 #define MTD_FD 4 99 #define TOTAL_FDS 5 100 101 #define MAPS_FLASH (1 << 0) 102 #define MAPS_MEM (1 << 1) 103 #define STATE_SUSPENDED (1 << 7) 104 enum mbox_state { 105 /* Still Initing */ 106 UNINITIALISED = 0, 107 /* Active and LPC Maps Flash */ 108 ACTIVE_MAPS_FLASH = MAPS_FLASH, 109 /* Suspended and LPC Maps Flash */ 110 SUSPEND_MAPS_FLASH = STATE_SUSPENDED | MAPS_FLASH, 111 /* Active and LPC Maps Memory */ 112 ACTIVE_MAPS_MEM = MAPS_MEM, 113 /* Suspended and LPC Maps Memory */ 114 SUSPEND_MAPS_MEM = STATE_SUSPENDED | MAPS_MEM 115 }; 116 117 #define FLASH_OFFSET_UNINIT 0xFFFFFFFF 118 119 struct window_context { 120 void *mem; /* Portion of Reserved Memory Region */ 121 uint32_t flash_offset; /* Flash area the window maps (bytes) */ 122 uint32_t size; /* Window Size (bytes) power-of-2 */ 123 uint8_t *dirty_bmap; /* Bytemap of the dirty/erased state */ 124 uint32_t age; /* Used for LRU eviction scheme */ 125 }; 126 127 struct window_list { 128 uint32_t num; 129 uint32_t max_age; 130 uint32_t default_size; 131 struct window_context *window; 132 }; 133 134 struct mbox_context { 135 /* System State */ 136 enum mbox_state state; 137 enum api_version version; 138 struct pollfd fds[TOTAL_FDS]; 139 sd_bus *bus; 140 bool terminate; 141 uint8_t bmc_events; 142 uint8_t prev_seq; 143 144 /* Window State */ 145 /* The window list struct containing all current "windows" */ 146 struct window_list windows; 147 /* The window the host is currently pointed at */ 148 struct window_context *current; 149 /* Is the current window a write one */ 150 bool current_is_write; 151 152 /* Memory & Flash State */ 153 /* Reserved Memory Region */ 154 void *mem; 155 /* Reserved Mem Size (bytes) */ 156 uint32_t mem_size; 157 /* LPC Bus Base Address (bytes) */ 158 uint32_t lpc_base; 159 /* Flash size from command line (bytes) */ 160 uint32_t flash_size; 161 /* Bytemap of the erased state of the entire flash */ 162 uint8_t *flash_bmap; 163 /* Erase size (as a shift) */ 164 uint32_t erase_size_shift; 165 /* Block size (as a shift) */ 166 uint32_t block_size_shift; 167 /* Actual Flash Info */ 168 struct mtd_info_user mtd_info; 169 }; 170 171 #endif /* MBOX_H */ 172