Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
| 3 | #include "config.h" |
| 4 | |
| 5 | #include <errno.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #include "mbox.h" |
| 9 | #include "lpc.h" |
| 10 | #include "transport_mbox.h" /* TODO: Remove dependency on transport_mbox.h */ |
| 11 | #include "windows.h" |
| 12 | |
Andrew Jeffery | ab666a5 | 2018-08-07 14:28:09 +0930 | [diff] [blame] | 13 | int protocol_v1_reset(struct mbox_context *context) |
| 14 | { |
| 15 | /* Host requested it -> No BMC Event */ |
| 16 | windows_reset_all(context, NO_BMC_EVENT); |
| 17 | return lpc_reset(context); |
| 18 | } |
| 19 | |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 20 | int protocol_v1_get_info(struct mbox_context *context, |
| 21 | struct protocol_get_info *io) |
| 22 | { |
| 23 | uint8_t old_version = context->version; |
| 24 | int rc; |
| 25 | |
| 26 | /* Bootstrap protocol version. This may involve {up,down}grading */ |
| 27 | rc = protocol_negotiate_version(context, io->req.api_version); |
| 28 | if (rc < 0) |
| 29 | return rc; |
| 30 | |
| 31 | /* Do the {up,down}grade if necessary*/ |
| 32 | if (rc != old_version) { |
| 33 | windows_reset_all(context, SET_BMC_EVENT); |
| 34 | return context->protocol->get_info(context, io); |
| 35 | } |
| 36 | |
| 37 | /* Record the negotiated version for the response */ |
| 38 | io->resp.api_version = rc; |
| 39 | |
| 40 | /* Now do all required intialisation for v1 */ |
| 41 | context->block_size_shift = BLOCK_SIZE_SHIFT_V1; |
| 42 | MSG_INFO("Block Size: 0x%.8x (shift: %u)\n", |
| 43 | 1 << context->block_size_shift, context->block_size_shift); |
| 44 | |
| 45 | /* Knowing blocksize we can allocate the window dirty_bytemap */ |
| 46 | windows_alloc_dirty_bytemap(context); |
| 47 | |
| 48 | io->resp.v1.read_window_size = |
| 49 | context->windows.default_size >> context->block_size_shift; |
| 50 | io->resp.v1.write_window_size = |
| 51 | context->windows.default_size >> context->block_size_shift; |
| 52 | |
| 53 | return lpc_map_memory(context); |
| 54 | } |
| 55 | |
Andrew Jeffery | 91a8745 | 2018-08-07 14:54:14 +0930 | [diff] [blame] | 56 | int protocol_v1_get_flash_info(struct mbox_context *context, |
| 57 | struct protocol_get_flash_info *io) |
| 58 | { |
| 59 | io->resp.v1.flash_size = context->flash_size; |
| 60 | io->resp.v1.erase_size = context->mtd_info.erasesize; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 65 | /* |
Andrew Jeffery | 22fa500 | 2018-08-07 15:22:50 +0930 | [diff] [blame] | 66 | * get_lpc_addr_shifted() - Get lpc address of the current window |
| 67 | * @context: The mbox context pointer |
| 68 | * |
| 69 | * Return: The lpc address to access that offset shifted by block size |
| 70 | */ |
| 71 | static inline uint16_t get_lpc_addr_shifted(struct mbox_context *context) |
| 72 | { |
| 73 | uint32_t lpc_addr, mem_offset; |
| 74 | |
| 75 | /* Offset of the current window in the reserved memory region */ |
| 76 | mem_offset = context->current->mem - context->mem; |
| 77 | /* Total LPC Address */ |
| 78 | lpc_addr = context->lpc_base + mem_offset; |
| 79 | |
| 80 | MSG_DBG("LPC address of current window: 0x%.8x\n", lpc_addr); |
| 81 | |
| 82 | return lpc_addr >> context->block_size_shift; |
| 83 | } |
| 84 | |
Andrew Jeffery | 4bcec8e | 2018-08-07 15:33:41 +0930 | [diff] [blame] | 85 | int protocol_v1_create_window(struct mbox_context *context, |
| 86 | struct protocol_create_window *io) |
Andrew Jeffery | 22fa500 | 2018-08-07 15:22:50 +0930 | [diff] [blame] | 87 | { |
| 88 | int rc; |
| 89 | uint32_t offset = io->req.offset << context->block_size_shift; |
| 90 | |
| 91 | /* Close the current window if there is one */ |
| 92 | if (context->current) { |
| 93 | /* There is an implicit flush if it was a write window */ |
| 94 | if (context->current_is_write) { |
| 95 | rc = mbox_handle_flush_window(context, NULL, NULL); |
| 96 | if (rc < 0) { |
| 97 | MSG_ERR("Couldn't Flush Write Window\n"); |
| 98 | return rc; |
| 99 | } |
| 100 | } |
| 101 | windows_close_current(context, NO_BMC_EVENT, FLAGS_NONE); |
| 102 | } |
| 103 | |
| 104 | /* Offset the host has requested */ |
| 105 | MSG_INFO("Host requested flash @ 0x%.8x\n", offset); |
| 106 | /* Check if we have an existing window */ |
| 107 | context->current = windows_search(context, offset, |
| 108 | context->version == API_VERSION_1); |
| 109 | |
| 110 | if (!context->current) { /* No existing window */ |
| 111 | MSG_DBG("No existing window which maps that flash offset\n"); |
| 112 | rc = windows_create_map(context, &context->current, |
| 113 | offset, |
| 114 | context->version == API_VERSION_1); |
| 115 | if (rc < 0) { /* Unable to map offset */ |
| 116 | MSG_ERR("Couldn't create window mapping for offset 0x%.8x\n", |
Andrew Jeffery | 4bcec8e | 2018-08-07 15:33:41 +0930 | [diff] [blame] | 117 | offset); |
Andrew Jeffery | 22fa500 | 2018-08-07 15:22:50 +0930 | [diff] [blame] | 118 | return rc; |
| 119 | } |
| 120 | } |
| 121 | |
Andrew Jeffery | 4bcec8e | 2018-08-07 15:33:41 +0930 | [diff] [blame] | 122 | context->current_is_write = !io->req.ro; |
| 123 | |
Andrew Jeffery | 22fa500 | 2018-08-07 15:22:50 +0930 | [diff] [blame] | 124 | MSG_INFO("Window @ %p for size 0x%.8x maps flash offset 0x%.8x\n", |
| 125 | context->current->mem, context->current->size, |
| 126 | context->current->flash_offset); |
| 127 | |
| 128 | io->resp.lpc_address = get_lpc_addr_shifted(context); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Andrew Jeffery | a336e43 | 2018-08-07 16:00:40 +0930 | [diff] [blame^] | 133 | int protocol_v1_mark_dirty(struct mbox_context *context, |
| 134 | struct protocol_mark_dirty *io) |
| 135 | { |
| 136 | uint32_t offset = io->req.v1.offset; |
| 137 | uint32_t size = io->req.v1.size; |
| 138 | uint32_t off; |
| 139 | |
| 140 | if (!(context->current && context->current_is_write)) { |
| 141 | MSG_ERR("Tried to call mark dirty without open write window\n"); |
| 142 | return -EPERM; |
| 143 | } |
| 144 | |
| 145 | /* For V1 offset given relative to flash - we want the window */ |
| 146 | off = offset - ((context->current->flash_offset) >> |
| 147 | context->block_size_shift); |
| 148 | if (off > offset) { /* Underflow - before current window */ |
| 149 | MSG_ERR("Tried to mark dirty before start of window\n"); |
| 150 | MSG_ERR("requested offset: 0x%x window start: 0x%x\n", |
| 151 | offset << context->block_size_shift, |
| 152 | context->current->flash_offset); |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | offset = off; |
| 156 | /* |
| 157 | * We only track dirty at the block level. |
| 158 | * For protocol V1 we can get away with just marking the whole |
| 159 | * block dirty. |
| 160 | */ |
| 161 | size = align_up(size, 1 << context->block_size_shift); |
| 162 | size >>= context->block_size_shift; |
| 163 | |
| 164 | MSG_INFO("Dirty window @ 0x%.8x for 0x%.8x\n", |
| 165 | offset << context->block_size_shift, |
| 166 | size << context->block_size_shift); |
| 167 | |
| 168 | return window_set_bytemap(context, context->current, offset, size, |
| 169 | WINDOW_DIRTY); |
| 170 | } |
| 171 | |
Andrew Jeffery | 22fa500 | 2018-08-07 15:22:50 +0930 | [diff] [blame] | 172 | /* |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 173 | * get_suggested_timeout() - get the suggested timeout value in seconds |
| 174 | * @context: The mbox context pointer |
| 175 | * |
| 176 | * Return: Suggested timeout in seconds |
| 177 | */ |
| 178 | static uint16_t get_suggested_timeout(struct mbox_context *context) |
| 179 | { |
| 180 | struct window_context *window = windows_find_largest(context); |
| 181 | uint32_t max_size_mb = window ? (window->size >> 20) : 0; |
| 182 | uint16_t ret; |
| 183 | |
| 184 | ret = align_up(max_size_mb * FLASH_ACCESS_MS_PER_MB, 1000) / 1000; |
| 185 | |
| 186 | MSG_DBG("Suggested Timeout: %us, max window size: %uMB, for %dms/MB\n", |
| 187 | ret, max_size_mb, FLASH_ACCESS_MS_PER_MB); |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | int protocol_v2_get_info(struct mbox_context *context, |
| 192 | struct protocol_get_info *io) |
| 193 | { |
| 194 | uint8_t old_version = context->version; |
| 195 | int rc; |
| 196 | |
| 197 | /* Bootstrap protocol version. This may involve {up,down}grading */ |
| 198 | rc = protocol_negotiate_version(context, io->req.api_version); |
| 199 | if (rc < 0) |
| 200 | return rc; |
| 201 | |
| 202 | /* Do the {up,down}grade if necessary*/ |
| 203 | if (rc != old_version) { |
| 204 | windows_reset_all(context, SET_BMC_EVENT); |
| 205 | return context->protocol->get_info(context, io); |
| 206 | } |
| 207 | |
| 208 | /* Record the negotiated version for the response */ |
| 209 | io->resp.api_version = rc; |
| 210 | |
| 211 | /* Now do all required intialisation for v2 */ |
| 212 | context->block_size_shift = log_2(context->mtd_info.erasesize); |
| 213 | MSG_INFO("Block Size: 0x%.8x (shift: %u)\n", |
| 214 | 1 << context->block_size_shift, context->block_size_shift); |
| 215 | |
| 216 | /* Knowing blocksize we can allocate the window dirty_bytemap */ |
| 217 | windows_alloc_dirty_bytemap(context); |
| 218 | |
| 219 | io->resp.v2.block_size_shift = context->block_size_shift; |
| 220 | io->resp.v2.timeout = get_suggested_timeout(context); |
| 221 | |
| 222 | return lpc_map_memory(context); |
| 223 | } |
| 224 | |
Andrew Jeffery | 91a8745 | 2018-08-07 14:54:14 +0930 | [diff] [blame] | 225 | int protocol_v2_get_flash_info(struct mbox_context *context, |
| 226 | struct protocol_get_flash_info *io) |
| 227 | { |
| 228 | io->resp.v2.flash_size = |
| 229 | context->flash_size >> context->block_size_shift; |
| 230 | io->resp.v2.erase_size = |
| 231 | context->mtd_info.erasesize >> context->block_size_shift; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Andrew Jeffery | 4bcec8e | 2018-08-07 15:33:41 +0930 | [diff] [blame] | 236 | int protocol_v2_create_window(struct mbox_context *context, |
| 237 | struct protocol_create_window *io) |
Andrew Jeffery | 22fa500 | 2018-08-07 15:22:50 +0930 | [diff] [blame] | 238 | { |
| 239 | int rc; |
| 240 | |
Andrew Jeffery | 4bcec8e | 2018-08-07 15:33:41 +0930 | [diff] [blame] | 241 | rc = protocol_v1_create_window(context, io); |
Andrew Jeffery | 22fa500 | 2018-08-07 15:22:50 +0930 | [diff] [blame] | 242 | if (rc < 0) |
| 243 | return rc; |
| 244 | |
| 245 | io->resp.size = context->current->size >> context->block_size_shift; |
| 246 | io->resp.offset = context->current->flash_offset >> |
| 247 | context->block_size_shift; |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Andrew Jeffery | a336e43 | 2018-08-07 16:00:40 +0930 | [diff] [blame^] | 252 | int protocol_v2_mark_dirty(struct mbox_context *context, |
| 253 | struct protocol_mark_dirty *io) |
| 254 | { |
| 255 | if (!(context->current && context->current_is_write)) { |
| 256 | MSG_ERR("Tried to call mark dirty without open write window\n"); |
| 257 | return -EPERM; |
| 258 | } |
| 259 | |
| 260 | MSG_INFO("Dirty window @ 0x%.8x for 0x%.8x\n", |
| 261 | io->req.v2.offset << context->block_size_shift, |
| 262 | io->req.v2.size << context->block_size_shift); |
| 263 | |
| 264 | return window_set_bytemap(context, context->current, io->req.v2.offset, |
| 265 | io->req.v2.size, WINDOW_DIRTY); |
| 266 | } |
| 267 | |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 268 | static const struct protocol_ops protocol_ops_v1 = { |
Andrew Jeffery | ab666a5 | 2018-08-07 14:28:09 +0930 | [diff] [blame] | 269 | .reset = protocol_v1_reset, |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 270 | .get_info = protocol_v1_get_info, |
Andrew Jeffery | 91a8745 | 2018-08-07 14:54:14 +0930 | [diff] [blame] | 271 | .get_flash_info = protocol_v1_get_flash_info, |
Andrew Jeffery | 4bcec8e | 2018-08-07 15:33:41 +0930 | [diff] [blame] | 272 | .create_window = protocol_v1_create_window, |
Andrew Jeffery | a336e43 | 2018-08-07 16:00:40 +0930 | [diff] [blame^] | 273 | .mark_dirty = protocol_v1_mark_dirty, |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | static const struct protocol_ops protocol_ops_v2 = { |
Andrew Jeffery | ab666a5 | 2018-08-07 14:28:09 +0930 | [diff] [blame] | 277 | .reset = protocol_v1_reset, |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 278 | .get_info = protocol_v2_get_info, |
Andrew Jeffery | 91a8745 | 2018-08-07 14:54:14 +0930 | [diff] [blame] | 279 | .get_flash_info = protocol_v2_get_flash_info, |
Andrew Jeffery | 4bcec8e | 2018-08-07 15:33:41 +0930 | [diff] [blame] | 280 | .create_window = protocol_v2_create_window, |
Andrew Jeffery | a336e43 | 2018-08-07 16:00:40 +0930 | [diff] [blame^] | 281 | .mark_dirty = protocol_v2_mark_dirty, |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | static const struct protocol_ops *protocol_ops_map[] = { |
| 285 | [0] = NULL, |
| 286 | [1] = &protocol_ops_v1, |
| 287 | [2] = &protocol_ops_v2, |
| 288 | }; |
| 289 | |
| 290 | int protocol_negotiate_version(struct mbox_context *context, |
| 291 | uint8_t requested) |
| 292 | { |
| 293 | /* Check we support the version requested */ |
| 294 | if (requested < API_MIN_VERSION) |
| 295 | return -EINVAL; |
| 296 | |
| 297 | context->version = (requested > API_MAX_VERSION) ? |
| 298 | API_MAX_VERSION : requested; |
| 299 | |
| 300 | context->protocol = protocol_ops_map[context->version]; |
| 301 | |
| 302 | return context->version; |
| 303 | } |
| 304 | |
| 305 | int protocol_init(struct mbox_context *context) |
| 306 | { |
| 307 | context->version = API_MAX_VERSION; |
| 308 | context->protocol = protocol_ops_map[context->version]; |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | void protocol_free(struct mbox_context *context) |
| 314 | { |
| 315 | return; |
| 316 | } |