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 | |
| 121 | struct mbox_context { |
| 122 | /* System State */ |
| 123 | enum mbox_state state; |
| 124 | enum api_version version; |
| 125 | struct pollfd fds[TOTAL_FDS]; |
| 126 | sd_bus *bus; |
| 127 | bool terminate; |
| 128 | uint8_t bmc_events; |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 129 | uint8_t prev_seq; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 130 | |
| 131 | /* Window State */ |
| 132 | /* The window list struct containing all current "windows" */ |
| 133 | struct window_list windows; |
| 134 | /* The window the host is currently pointed at */ |
| 135 | struct window_context *current; |
| 136 | /* Is the current window a write one */ |
| 137 | bool current_is_write; |
| 138 | |
| 139 | /* Memory & Flash State */ |
| 140 | /* Reserved Memory Region */ |
| 141 | void *mem; |
| 142 | /* Reserved Mem Size (bytes) */ |
| 143 | uint32_t mem_size; |
| 144 | /* LPC Bus Base Address (bytes) */ |
| 145 | uint32_t lpc_base; |
| 146 | /* Flash size from command line (bytes) */ |
| 147 | uint32_t flash_size; |
| 148 | /* Bytemap of the erased state of the entire flash */ |
| 149 | uint8_t *flash_bmap; |
| 150 | /* Erase size (as a shift) */ |
| 151 | uint32_t erase_size_shift; |
| 152 | /* Block size (as a shift) */ |
| 153 | uint32_t block_size_shift; |
| 154 | /* Actual Flash Info */ |
| 155 | struct mtd_info_user mtd_info; |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 156 | #ifdef VIRTUAL_PNOR_ENABLED |
| 157 | /* Virtual PNOR partition table */ |
| 158 | struct vpnor_partition_table *vpnor; |
Ratan Gupta | 8441a39 | 2017-05-05 21:42:53 +0530 | [diff] [blame] | 159 | struct vpnor_partition_paths paths; |
Deepak Kodihalli | b6a446f | 2017-04-29 13:01:49 -0500 | [diff] [blame] | 160 | #endif |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | #endif /* MBOX_H */ |