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 | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 11 | |
| 12 | #include "protocol.h" |
Andrew Jeffery | 53c21aa | 2018-03-26 11:56:16 +1030 | [diff] [blame] | 13 | #include "vpnor/mboxd_pnor_partition_table.h" |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 14 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 15 | enum api_version { |
| 16 | API_VERSION_INVAL = 0, |
| 17 | API_VERSION_1 = 1, |
| 18 | API_VERSION_2 = 2 |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 19 | }; |
| 20 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 21 | #define API_MIN_VERSION API_VERSION_1 |
| 22 | #define API_MAX_VERSION API_VERSION_2 |
| 23 | |
| 24 | #define THIS_NAME "Mailbox Daemon" |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 25 | |
| 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 Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 47 | #define MBOX_R_SEQ_ERROR 0x08 |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 48 | |
| 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) |
| 93 | enum 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 Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 104 | }; |
| 105 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 106 | #define FLASH_OFFSET_UNINIT 0xFFFFFFFF |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 107 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 108 | struct 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 | |
| 116 | struct window_list { |
| 117 | uint32_t num; |
| 118 | uint32_t max_age; |
| 119 | uint32_t default_size; |
| 120 | struct window_context *window; |
| 121 | }; |
| 122 | |
Andrew Jeffery | efb09de | 2018-03-26 14:36:43 +1030 | [diff] [blame] | 123 | struct mbox_msg { |
| 124 | uint8_t command; |
| 125 | uint8_t seq; |
| 126 | uint8_t args[MBOX_ARGS_BYTES]; |
| 127 | uint8_t response; |
| 128 | }; |
| 129 | |
| 130 | union mbox_regs { |
| 131 | uint8_t raw[MBOX_REG_BYTES]; |
| 132 | struct mbox_msg msg; |
| 133 | }; |
| 134 | |
Andrew Jeffery | 5b7b018 | 2018-06-06 17:15:38 +0930 | [diff] [blame] | 135 | struct mbox_context; |
Andrew Jeffery | 22fa500 | 2018-08-07 15:22:50 +0930 | [diff] [blame] | 136 | |
Andrew Jeffery | efb09de | 2018-03-26 14:36:43 +1030 | [diff] [blame] | 137 | typedef int (*mboxd_mbox_handler)(struct mbox_context *, union mbox_regs *, |
| 138 | struct mbox_msg *); |
| 139 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 140 | struct mbox_context { |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 141 | enum api_version version; |
| 142 | const struct protocol_ops *protocol; |
| 143 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 144 | /* System State */ |
| 145 | enum mbox_state state; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 146 | struct pollfd fds[TOTAL_FDS]; |
| 147 | sd_bus *bus; |
| 148 | bool terminate; |
| 149 | uint8_t bmc_events; |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 150 | uint8_t prev_seq; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 151 | |
Andrew Jeffery | efb09de | 2018-03-26 14:36:43 +1030 | [diff] [blame] | 152 | /* Command Dispatch */ |
| 153 | const mboxd_mbox_handler *handlers; |
| 154 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 155 | /* 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 Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 180 | #ifdef VIRTUAL_PNOR_ENABLED |
| 181 | /* Virtual PNOR partition table */ |
| 182 | struct vpnor_partition_table *vpnor; |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 183 | struct vpnor_partition_paths paths; |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 184 | #endif |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | #endif /* MBOX_H */ |