Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 */ |
| 2 | /* Copyright (C) 2018 IBM Corp. */ |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 3 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 4 | #ifndef MBOX_H |
| 5 | #define MBOX_H |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 6 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 7 | #include <mtd/mtd-abi.h> |
| 8 | #include <systemd/sd-bus.h> |
Andrew Jeffery | 8ecbdb5 | 2017-04-10 16:26:08 +0930 | [diff] [blame] | 9 | #include <poll.h> |
| 10 | #include <stdbool.h> |
Andrew Jeffery | 53c21aa | 2018-03-26 11:56:16 +1030 | [diff] [blame] | 11 | #include "vpnor/mboxd_pnor_partition_table.h" |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 12 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 13 | enum api_version { |
| 14 | API_VERSION_INVAL = 0, |
| 15 | API_VERSION_1 = 1, |
| 16 | API_VERSION_2 = 2 |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 17 | }; |
| 18 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 19 | #define API_MIN_VERSION API_VERSION_1 |
| 20 | #define API_MAX_VERSION API_VERSION_2 |
| 21 | |
| 22 | #define THIS_NAME "Mailbox Daemon" |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 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 |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 45 | #define MBOX_R_SEQ_ERROR 0x08 |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 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 |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 102 | }; |
| 103 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 104 | #define FLASH_OFFSET_UNINIT 0xFFFFFFFF |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 105 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 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 | |
Andrew Jeffery | efb09de | 2018-03-26 14:36:43 +1030 | [diff] [blame] | 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 | typedef int (*mboxd_mbox_handler)(struct mbox_context *, union mbox_regs *, |
| 134 | struct mbox_msg *); |
| 135 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 136 | struct mbox_context { |
| 137 | /* System State */ |
| 138 | enum mbox_state state; |
| 139 | enum api_version version; |
| 140 | struct pollfd fds[TOTAL_FDS]; |
| 141 | sd_bus *bus; |
| 142 | bool terminate; |
| 143 | uint8_t bmc_events; |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 144 | uint8_t prev_seq; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 145 | |
Andrew Jeffery | efb09de | 2018-03-26 14:36:43 +1030 | [diff] [blame] | 146 | /* Command Dispatch */ |
| 147 | const mboxd_mbox_handler *handlers; |
| 148 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 149 | /* Window State */ |
| 150 | /* The window list struct containing all current "windows" */ |
| 151 | struct window_list windows; |
| 152 | /* The window the host is currently pointed at */ |
| 153 | struct window_context *current; |
| 154 | /* Is the current window a write one */ |
| 155 | bool current_is_write; |
| 156 | |
| 157 | /* Memory & Flash State */ |
| 158 | /* Reserved Memory Region */ |
| 159 | void *mem; |
| 160 | /* Reserved Mem Size (bytes) */ |
| 161 | uint32_t mem_size; |
| 162 | /* LPC Bus Base Address (bytes) */ |
| 163 | uint32_t lpc_base; |
| 164 | /* Flash size from command line (bytes) */ |
| 165 | uint32_t flash_size; |
| 166 | /* Bytemap of the erased state of the entire flash */ |
| 167 | uint8_t *flash_bmap; |
| 168 | /* Erase size (as a shift) */ |
| 169 | uint32_t erase_size_shift; |
| 170 | /* Block size (as a shift) */ |
| 171 | uint32_t block_size_shift; |
| 172 | /* Actual Flash Info */ |
| 173 | struct mtd_info_user mtd_info; |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 174 | #ifdef VIRTUAL_PNOR_ENABLED |
| 175 | /* Virtual PNOR partition table */ |
| 176 | struct vpnor_partition_table *vpnor; |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 177 | struct vpnor_partition_paths paths; |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 178 | #endif |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | #endif /* MBOX_H */ |