Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Mailbox Daemon MBOX Message Helpers |
| 3 | * |
| 4 | * Copyright 2016 IBM |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #define _GNU_SOURCE |
| 21 | #include <assert.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <getopt.h> |
| 25 | #include <limits.h> |
| 26 | #include <poll.h> |
| 27 | #include <stdbool.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <syslog.h> |
| 33 | #include <signal.h> |
| 34 | #include <sys/ioctl.h> |
| 35 | #include <sys/mman.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/timerfd.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <time.h> |
| 40 | #include <unistd.h> |
| 41 | #include <inttypes.h> |
| 42 | |
| 43 | #include "mbox.h" |
| 44 | #include "common.h" |
| 45 | #include "mboxd_msg.h" |
| 46 | #include "mboxd_windows.h" |
| 47 | #include "mboxd_lpc.h" |
| 48 | |
| 49 | static int mbox_handle_flush_window(struct mbox_context *context, union mbox_regs *req, |
| 50 | struct mbox_msg *resp); |
| 51 | |
| 52 | typedef int (*mboxd_mbox_handler)(struct mbox_context *, union mbox_regs *, |
| 53 | struct mbox_msg *); |
| 54 | |
| 55 | /* |
| 56 | * write_bmc_event_reg() - Write to the BMC controlled status register (reg 15) |
| 57 | * @context: The mbox context pointer |
| 58 | * |
| 59 | * Return: 0 on success otherwise negative error code |
| 60 | */ |
| 61 | static int write_bmc_event_reg(struct mbox_context *context) |
| 62 | { |
| 63 | int rc; |
| 64 | |
| 65 | /* Seek mbox registers */ |
| 66 | rc = lseek(context->fds[MBOX_FD].fd, MBOX_BMC_EVENT, SEEK_SET); |
| 67 | if (rc != MBOX_BMC_EVENT) { |
| 68 | MSG_ERR("Couldn't lseek mbox to byte %d: %s\n", MBOX_BMC_EVENT, |
| 69 | strerror(errno)); |
| 70 | return -MBOX_R_SYSTEM_ERROR; |
| 71 | } |
| 72 | |
| 73 | /* Write to mbox status register */ |
| 74 | rc = write(context->fds[MBOX_FD].fd, &context->bmc_events, 1); |
| 75 | if (rc != 1) { |
| 76 | MSG_ERR("Couldn't write to BMC status reg: %s\n", |
| 77 | strerror(errno)); |
| 78 | return -MBOX_R_SYSTEM_ERROR; |
| 79 | } |
| 80 | |
| 81 | /* Reset to start */ |
| 82 | rc = lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET); |
| 83 | if (rc) { |
| 84 | MSG_ERR("Couldn't reset MBOX offset to zero: %s\n", |
| 85 | strerror(errno)); |
| 86 | return -MBOX_R_SYSTEM_ERROR; |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * set_bmc_events() - Set BMC events |
| 94 | * @context: The mbox context pointer |
| 95 | * @bmc_event: The bits to set |
| 96 | * @write_back: Whether to write back to the register -> will interrupt host |
| 97 | * |
| 98 | * Return: 0 on success otherwise negative error code |
| 99 | */ |
| 100 | int set_bmc_events(struct mbox_context *context, uint8_t bmc_event, |
| 101 | bool write_back) |
| 102 | { |
| 103 | uint8_t mask = 0x00; |
| 104 | |
| 105 | switch (context->version) { |
| 106 | case API_VERSION_1: |
| 107 | mask = BMC_EVENT_V1_MASK; |
| 108 | break; |
| 109 | default: |
| 110 | mask = BMC_EVENT_V2_MASK; |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | context->bmc_events |= (bmc_event & mask); |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 115 | MSG_DBG("BMC Events set to: 0x%.2x\n", context->bmc_events); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 116 | |
| 117 | return write_back ? write_bmc_event_reg(context) : 0; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * clr_bmc_events() - Clear BMC events |
| 122 | * @context: The mbox context pointer |
| 123 | * @bmc_event: The bits to clear |
| 124 | * @write_back: Whether to write back to the register -> will interrupt host |
| 125 | * |
| 126 | * Return: 0 on success otherwise negative error code |
| 127 | */ |
| 128 | int clr_bmc_events(struct mbox_context *context, uint8_t bmc_event, |
| 129 | bool write_back) |
| 130 | { |
| 131 | context->bmc_events &= ~bmc_event; |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 132 | MSG_DBG("BMC Events clear to: 0x%.2x\n", context->bmc_events); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 133 | |
| 134 | return write_back ? write_bmc_event_reg(context) : 0; |
| 135 | } |
| 136 | |
| 137 | /* Command Handlers */ |
| 138 | |
| 139 | /* |
| 140 | * Command: RESET_STATE |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 141 | * Reset the LPC mapping to point back at the flash, or memory in case we're |
| 142 | * using a virtual pnor. |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 143 | */ |
| 144 | static int mbox_handle_reset(struct mbox_context *context, |
| 145 | union mbox_regs *req, struct mbox_msg *resp) |
| 146 | { |
| 147 | /* Host requested it -> No BMC Event */ |
| 148 | reset_all_windows(context, NO_BMC_EVENT); |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 149 | return reset_lpc(context); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
Suraj Jitindar Singh | 5a3a066 | 2017-04-27 11:55:26 +1000 | [diff] [blame] | 153 | * get_suggested_timeout() - get the suggested timeout value in seconds |
| 154 | * @context: The mbox context pointer |
| 155 | * |
| 156 | * Return: Suggested timeout in seconds |
| 157 | */ |
| 158 | static uint16_t get_suggested_timeout(struct mbox_context *context) |
| 159 | { |
| 160 | struct window_context *window = find_largest_window(context); |
| 161 | uint32_t max_size_mb = window ? (window->size >> 20) : 0; |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 162 | uint8_t ret; |
Suraj Jitindar Singh | 5a3a066 | 2017-04-27 11:55:26 +1000 | [diff] [blame] | 163 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 164 | ret = align_up(max_size_mb * FLASH_ACCESS_MS_PER_MB, 1000) / 1000; |
| 165 | |
| 166 | MSG_DBG("Suggested Timeout: %us, max window size: %uMB, for %dms/MB\n", |
| 167 | ret, max_size_mb, FLASH_ACCESS_MS_PER_MB); |
| 168 | return ret; |
Suraj Jitindar Singh | 5a3a066 | 2017-04-27 11:55:26 +1000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 172 | * Command: GET_MBOX_INFO |
| 173 | * Get the API version, default window size and block size |
| 174 | * We also set the LPC mapping to point to the reserved memory region here so |
| 175 | * this command must be called before any window manipulation |
| 176 | * |
| 177 | * V1: |
| 178 | * ARGS[0]: API Version |
| 179 | * |
| 180 | * RESP[0]: API Version |
| 181 | * RESP[1:2]: Default read window size (number of blocks) |
| 182 | * RESP[3:4]: Default write window size (number of blocks) |
| 183 | * RESP[5]: Block size (as shift) |
| 184 | * |
| 185 | * V2: |
| 186 | * ARGS[0]: API Version |
| 187 | * |
| 188 | * RESP[0]: API Version |
| 189 | * RESP[1:2]: Default read window size (number of blocks) |
| 190 | * RESP[3:4]: Default write window size (number of blocks) |
| 191 | * RESP[5]: Block size (as shift) |
| 192 | */ |
| 193 | static int mbox_handle_mbox_info(struct mbox_context *context, |
| 194 | union mbox_regs *req, struct mbox_msg *resp) |
| 195 | { |
| 196 | uint8_t mbox_api_version = req->msg.args[0]; |
| 197 | uint8_t old_api_version = context->version; |
| 198 | int rc; |
| 199 | |
| 200 | /* Check we support the version requested */ |
Andrew Jeffery | fb25aa7 | 2017-04-24 11:17:42 +0930 | [diff] [blame] | 201 | if (mbox_api_version < API_MIN_VERSION) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 202 | return -MBOX_R_PARAM_ERROR; |
Andrew Jeffery | fb25aa7 | 2017-04-24 11:17:42 +0930 | [diff] [blame] | 203 | |
| 204 | if (mbox_api_version > API_MAX_VERSION) |
| 205 | mbox_api_version = API_MAX_VERSION; |
| 206 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 207 | context->version = mbox_api_version; |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 208 | MSG_INFO("Using Protocol Version: %d\n", context->version); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * The reset state is currently to have the LPC bus point directly to |
| 212 | * flash, since we got a mbox_info command we know the host can talk |
| 213 | * mbox so point the LPC bus mapping to the reserved memory region now |
| 214 | * so the host can access what we put in it. |
| 215 | */ |
| 216 | rc = point_to_memory(context); |
| 217 | if (rc < 0) { |
| 218 | return rc; |
| 219 | } |
| 220 | |
| 221 | switch (context->version) { |
| 222 | case API_VERSION_1: |
| 223 | context->block_size_shift = BLOCK_SIZE_SHIFT_V1; |
| 224 | break; |
| 225 | default: |
| 226 | context->block_size_shift = log_2(context->mtd_info.erasesize); |
| 227 | break; |
| 228 | } |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 229 | MSG_INFO("Block Size: 0x%.8x (shift: %u)\n", |
| 230 | 1 << context->block_size_shift, context->block_size_shift); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 231 | |
| 232 | /* Now we know the blocksize we can allocate the window dirty_bytemap */ |
| 233 | if (mbox_api_version != old_api_version) { |
| 234 | alloc_window_dirty_bytemap(context); |
| 235 | } |
| 236 | /* Reset if we were V1 since this required exact window mapping */ |
| 237 | if (old_api_version == API_VERSION_1) { |
| 238 | /* |
| 239 | * This will only set the BMC event if there was a current |
| 240 | * window -> In which case we are better off notifying the |
| 241 | * host. |
| 242 | */ |
| 243 | reset_all_windows(context, SET_BMC_EVENT); |
| 244 | } |
| 245 | |
| 246 | resp->args[0] = mbox_api_version; |
| 247 | if (context->version == API_VERSION_1) { |
| 248 | put_u16(&resp->args[1], context->windows.default_size >> |
| 249 | context->block_size_shift); |
| 250 | put_u16(&resp->args[3], context->windows.default_size >> |
| 251 | context->block_size_shift); |
| 252 | } |
| 253 | if (context->version >= API_VERSION_2) { |
| 254 | resp->args[5] = context->block_size_shift; |
Suraj Jitindar Singh | 5a3a066 | 2017-04-27 11:55:26 +1000 | [diff] [blame] | 255 | put_u16(&resp->args[6], get_suggested_timeout(context)); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Command: GET_FLASH_INFO |
| 263 | * Get the flash size and erase granularity |
| 264 | * |
| 265 | * V1: |
| 266 | * RESP[0:3]: Flash Size (bytes) |
| 267 | * RESP[4:7]: Erase Size (bytes) |
| 268 | * V2: |
| 269 | * RESP[0:1]: Flash Size (number of blocks) |
| 270 | * RESP[2:3]: Erase Size (number of blocks) |
| 271 | */ |
| 272 | static int mbox_handle_flash_info(struct mbox_context *context, |
| 273 | union mbox_regs *req, struct mbox_msg *resp) |
| 274 | { |
| 275 | switch (context->version) { |
| 276 | case API_VERSION_1: |
| 277 | /* Both Sizes in Bytes */ |
| 278 | put_u32(&resp->args[0], context->flash_size); |
| 279 | put_u32(&resp->args[4], context->mtd_info.erasesize); |
| 280 | break; |
| 281 | case API_VERSION_2: |
| 282 | /* Both Sizes in Block Size */ |
| 283 | put_u16(&resp->args[0], |
| 284 | context->flash_size >> context->block_size_shift); |
| 285 | put_u16(&resp->args[2], |
| 286 | context->mtd_info.erasesize >> |
| 287 | context->block_size_shift); |
| 288 | break; |
| 289 | default: |
| 290 | MSG_ERR("API Version Not Valid - Invalid System State\n"); |
| 291 | return -MBOX_R_SYSTEM_ERROR; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * get_lpc_addr_shifted() - Get lpc address of the current window |
| 299 | * @context: The mbox context pointer |
| 300 | * |
| 301 | * Return: The lpc address to access that offset shifted by block size |
| 302 | */ |
| 303 | static inline uint16_t get_lpc_addr_shifted(struct mbox_context *context) |
| 304 | { |
| 305 | uint32_t lpc_addr, mem_offset; |
| 306 | |
| 307 | /* Offset of the current window in the reserved memory region */ |
| 308 | mem_offset = context->current->mem - context->mem; |
| 309 | /* Total LPC Address */ |
| 310 | lpc_addr = context->lpc_base + mem_offset; |
| 311 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 312 | MSG_DBG("LPC address of current window: 0x%.8x\n", lpc_addr); |
| 313 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 314 | return lpc_addr >> context->block_size_shift; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Command: CREATE_READ_WINDOW |
| 319 | * Opens a read window |
| 320 | * First checks if any current window with the requested data, if so we just |
| 321 | * point the host to that. Otherwise we read the request data in from flash and |
| 322 | * point the host there. |
| 323 | * |
| 324 | * V1: |
| 325 | * ARGS[0:1]: Window Location as Offset into Flash (number of blocks) |
| 326 | * |
| 327 | * RESP[0:1]: LPC bus address for host to access this window (number of blocks) |
| 328 | * |
| 329 | * V2: |
| 330 | * ARGS[0:1]: Window Location as Offset into Flash (number of blocks) |
| 331 | * ARGS[2:3]: Requested window size (number of blocks) |
| 332 | * |
| 333 | * RESP[0:1]: LPC bus address for host to access this window (number of blocks) |
| 334 | * RESP[2:3]: Actual window size that the host can access (number of blocks) |
| 335 | */ |
| 336 | static int mbox_handle_read_window(struct mbox_context *context, |
| 337 | union mbox_regs *req, struct mbox_msg *resp) |
| 338 | { |
| 339 | uint32_t flash_offset; |
| 340 | int rc; |
| 341 | |
| 342 | /* Close the current window if there is one */ |
| 343 | if (context->current) { |
| 344 | /* There is an implicit flush if it was a write window */ |
| 345 | if (context->current_is_write) { |
| 346 | rc = mbox_handle_flush_window(context, NULL, NULL); |
| 347 | if (rc < 0) { |
| 348 | MSG_ERR("Couldn't Flush Write Window\n"); |
| 349 | return rc; |
| 350 | } |
| 351 | } |
| 352 | close_current_window(context, NO_BMC_EVENT, FLAGS_NONE); |
| 353 | } |
| 354 | |
| 355 | /* Offset the host has requested */ |
| 356 | flash_offset = get_u16(&req->msg.args[0]) << context->block_size_shift; |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 357 | MSG_INFO("Host requested flash @ 0x%.8x\n", flash_offset); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 358 | /* Check if we have an existing window */ |
| 359 | context->current = search_windows(context, flash_offset, |
| 360 | context->version == API_VERSION_1); |
| 361 | |
| 362 | if (!context->current) { /* No existing window */ |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 363 | MSG_DBG("No existing window which maps that flash offset\n"); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 364 | rc = create_map_window(context, &context->current, flash_offset, |
| 365 | context->version == API_VERSION_1); |
| 366 | if (rc < 0) { /* Unable to map offset */ |
| 367 | MSG_ERR("Couldn't create window mapping for offset 0x%.8x\n" |
| 368 | , flash_offset); |
| 369 | return rc; |
| 370 | } |
| 371 | } |
| 372 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 373 | MSG_INFO("Window @ %p for size 0x%.8x maps flash offset 0x%.8x\n", |
| 374 | context->current->mem, context->current->size, |
| 375 | context->current->flash_offset); |
| 376 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 377 | put_u16(&resp->args[0], get_lpc_addr_shifted(context)); |
| 378 | if (context->version >= API_VERSION_2) { |
| 379 | put_u16(&resp->args[2], context->current->size >> |
| 380 | context->block_size_shift); |
| 381 | put_u16(&resp->args[4], context->current->flash_offset >> |
| 382 | context->block_size_shift); |
| 383 | } |
| 384 | |
| 385 | context->current_is_write = false; |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Command: CREATE_WRITE_WINDOW |
| 392 | * Opens a write window |
| 393 | * First checks if any current window with the requested data, if so we just |
| 394 | * point the host to that. Otherwise we read the request data in from flash and |
| 395 | * point the host there. |
| 396 | * |
| 397 | * V1: |
| 398 | * ARGS[0:1]: Window Location as Offset into Flash (number of blocks) |
| 399 | * |
| 400 | * RESP[0:1]: LPC bus address for host to access this window (number of blocks) |
| 401 | * |
| 402 | * V2: |
| 403 | * ARGS[0:1]: Window Location as Offset into Flash (number of blocks) |
| 404 | * ARGS[2:3]: Requested window size (number of blocks) |
| 405 | * |
| 406 | * RESP[0:1]: LPC bus address for host to access this window (number of blocks) |
| 407 | * RESP[2:3]: Actual window size that was mapped/host can access (n.o. blocks) |
| 408 | */ |
| 409 | static int mbox_handle_write_window(struct mbox_context *context, |
| 410 | union mbox_regs *req, struct mbox_msg *resp) |
| 411 | { |
| 412 | int rc; |
| 413 | |
| 414 | /* |
| 415 | * This is very similar to opening a read window (exactly the same |
| 416 | * for now infact) |
| 417 | */ |
| 418 | rc = mbox_handle_read_window(context, req, resp); |
| 419 | if (rc < 0) { |
| 420 | return rc; |
| 421 | } |
| 422 | |
| 423 | context->current_is_write = true; |
| 424 | return rc; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Commands: MARK_WRITE_DIRTY |
| 429 | * Marks a portion of the current (write) window dirty, informing the daemon |
| 430 | * that is has been written to and thus must be at some point written to the |
| 431 | * backing store |
| 432 | * These changes aren't written back to the backing store unless flush is then |
| 433 | * called or the window closed |
| 434 | * |
| 435 | * V1: |
| 436 | * ARGS[0:1]: Where within flash to start (number of blocks) |
| 437 | * ARGS[2:5]: Number to mark dirty (number of bytes) |
| 438 | * |
| 439 | * V2: |
| 440 | * ARGS[0:1]: Where within window to start (number of blocks) |
| 441 | * ARGS[2:3]: Number to mark dirty (number of blocks) |
| 442 | */ |
| 443 | static int mbox_handle_dirty_window(struct mbox_context *context, |
| 444 | union mbox_regs *req, struct mbox_msg *resp) |
| 445 | { |
| 446 | uint32_t offset, size; |
| 447 | |
| 448 | if (!(context->current && context->current_is_write)) { |
| 449 | MSG_ERR("Tried to call mark dirty without open write window\n"); |
| 450 | return context->version >= API_VERSION_2 ? -MBOX_R_WINDOW_ERROR |
| 451 | : -MBOX_R_PARAM_ERROR; |
| 452 | } |
| 453 | |
| 454 | offset = get_u16(&req->msg.args[0]); |
| 455 | |
| 456 | if (context->version >= API_VERSION_2) { |
| 457 | size = get_u16(&req->msg.args[2]); |
| 458 | } else { |
| 459 | uint32_t off; |
| 460 | /* For V1 offset given relative to flash - we want the window */ |
| 461 | off = offset - ((context->current->flash_offset) >> |
| 462 | context->block_size_shift); |
| 463 | if (off > offset) { /* Underflow - before current window */ |
| 464 | MSG_ERR("Tried to mark dirty before start of window\n"); |
| 465 | MSG_ERR("requested offset: 0x%x window start: 0x%x\n", |
| 466 | offset << context->block_size_shift, |
| 467 | context->current->flash_offset); |
| 468 | return -MBOX_R_PARAM_ERROR; |
| 469 | } |
| 470 | offset = off; |
| 471 | size = get_u32(&req->msg.args[2]); |
| 472 | /* |
| 473 | * We only track dirty at the block level. |
| 474 | * For protocol V1 we can get away with just marking the whole |
| 475 | * block dirty. |
| 476 | */ |
| 477 | size = align_up(size, 1 << context->block_size_shift); |
| 478 | size >>= context->block_size_shift; |
| 479 | } |
| 480 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 481 | MSG_INFO("Dirty window @ 0x%.8x for 0x%.8x\n", |
| 482 | offset << context->block_size_shift, |
| 483 | size << context->block_size_shift); |
| 484 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 485 | return set_window_bytemap(context, context->current, offset, size, |
| 486 | WINDOW_DIRTY); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Commands: MARK_WRITE_ERASE |
| 491 | * Erases a portion of the current window |
| 492 | * These changes aren't written back to the backing store unless flush is then |
| 493 | * called or the window closed |
| 494 | * |
| 495 | * V1: |
| 496 | * Unimplemented |
| 497 | * |
| 498 | * V2: |
| 499 | * ARGS[0:1]: Where within window to start (number of blocks) |
| 500 | * ARGS[2:3]: Number to erase (number of blocks) |
| 501 | */ |
| 502 | static int mbox_handle_erase_window(struct mbox_context *context, |
| 503 | union mbox_regs *req, struct mbox_msg *resp) |
| 504 | { |
| 505 | uint32_t offset, size; |
| 506 | int rc; |
| 507 | |
| 508 | if (context->version < API_VERSION_2) { |
| 509 | MSG_ERR("Protocol Version invalid for Erase Command\n"); |
| 510 | return -MBOX_R_PARAM_ERROR; |
| 511 | } |
| 512 | |
| 513 | if (!(context->current && context->current_is_write)) { |
| 514 | MSG_ERR("Tried to call erase without open write window\n"); |
| 515 | return -MBOX_R_WINDOW_ERROR; |
| 516 | } |
| 517 | |
| 518 | offset = get_u16(&req->msg.args[0]); |
| 519 | size = get_u16(&req->msg.args[2]); |
| 520 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 521 | MSG_INFO("Erase window @ 0x%.8x for 0x%.8x\n", |
| 522 | offset << context->block_size_shift, |
| 523 | size << context->block_size_shift); |
| 524 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 525 | rc = set_window_bytemap(context, context->current, offset, size, |
| 526 | WINDOW_ERASED); |
| 527 | if (rc < 0) { |
| 528 | return rc; |
| 529 | } |
| 530 | |
| 531 | /* Write 0xFF to mem -> This ensures consistency between flash & ram */ |
| 532 | memset(context->current->mem + (offset << context->block_size_shift), |
| 533 | 0xFF, size << context->block_size_shift); |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Command: WRITE_FLUSH |
| 540 | * Flushes any dirty or erased blocks in the current window back to the backing |
| 541 | * store |
| 542 | * NOTE: For V1 this behaves much the same as the dirty command in that it |
| 543 | * takes an offset and number of blocks to dirty, then also performs a flush as |
| 544 | * part of the same command. For V2 this will only flush blocks already marked |
| 545 | * dirty/erased with the appropriate commands and doesn't take any arguments |
| 546 | * directly. |
| 547 | * |
| 548 | * V1: |
| 549 | * ARGS[0:1]: Where within window to start (number of blocks) |
| 550 | * ARGS[2:5]: Number to mark dirty (number of bytes) |
| 551 | * |
| 552 | * V2: |
| 553 | * NONE |
| 554 | */ |
| 555 | static int mbox_handle_flush_window(struct mbox_context *context, |
| 556 | union mbox_regs *req, struct mbox_msg *resp) |
| 557 | { |
| 558 | int rc, i, offset, count; |
| 559 | uint8_t prev; |
| 560 | |
| 561 | if (!(context->current && context->current_is_write)) { |
| 562 | MSG_ERR("Tried to call flush without open write window\n"); |
| 563 | return context->version >= API_VERSION_2 ? -MBOX_R_WINDOW_ERROR |
| 564 | : -MBOX_R_PARAM_ERROR; |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * For V1 the Flush command acts much the same as the dirty command |
| 569 | * except with a flush as well. Only do this on an actual flush |
| 570 | * command not when we call flush because we've implicitly closed a |
| 571 | * window because we might not have the required args in req. |
| 572 | */ |
| 573 | if (context->version == API_VERSION_1 && req && |
| 574 | req->msg.command == MBOX_C_WRITE_FLUSH) { |
| 575 | rc = mbox_handle_dirty_window(context, req, NULL); |
| 576 | if (rc < 0) { |
| 577 | return rc; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | offset = 0; |
| 582 | count = 0; |
| 583 | prev = WINDOW_CLEAN; |
| 584 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 585 | MSG_INFO("Flush window @ %p for size 0x%.8x which maps flash @ 0x%.8x\n", |
| 586 | context->current->mem, context->current->size, |
| 587 | context->current->flash_offset); |
| 588 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 589 | /* |
| 590 | * We look for streaks of the same type and keep a count, when the type |
| 591 | * (dirty/erased) changes we perform the required action on the backing |
| 592 | * store and update the current streak-type |
| 593 | */ |
| 594 | for (i = 0; i < (context->current->size >> context->block_size_shift); |
| 595 | i++) { |
| 596 | uint8_t cur = context->current->dirty_bmap[i]; |
| 597 | if (cur != WINDOW_CLEAN) { |
| 598 | if (cur == prev) { /* Same as previous block, incrmnt */ |
| 599 | count++; |
| 600 | } else if (prev == WINDOW_CLEAN) { /* Start of run */ |
| 601 | offset = i; |
| 602 | count++; |
| 603 | } else { /* Change in streak type */ |
| 604 | rc = write_from_window(context, offset, count, |
| 605 | prev); |
| 606 | if (rc < 0) { |
| 607 | return rc; |
| 608 | } |
| 609 | offset = i; |
| 610 | count = 1; |
| 611 | } |
| 612 | } else { |
| 613 | if (prev != WINDOW_CLEAN) { /* End of a streak */ |
| 614 | rc = write_from_window(context, offset, count, |
| 615 | prev); |
| 616 | if (rc < 0) { |
| 617 | return rc; |
| 618 | } |
| 619 | offset = 0; |
| 620 | count = 0; |
| 621 | } |
| 622 | } |
| 623 | prev = cur; |
| 624 | } |
| 625 | |
| 626 | if (prev != WINDOW_CLEAN) { /* Still the last streak to write */ |
| 627 | rc = write_from_window(context, offset, count, prev); |
| 628 | if (rc < 0) { |
| 629 | return rc; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /* Clear the dirty bytemap since we have written back all changes */ |
| 634 | return set_window_bytemap(context, context->current, 0, |
| 635 | context->current->size >> |
| 636 | context->block_size_shift, |
| 637 | WINDOW_CLEAN); |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * Command: CLOSE_WINDOW |
| 642 | * Close the current window |
| 643 | * NOTE: There is an implicit flush |
| 644 | * |
| 645 | * V1: |
| 646 | * NONE |
| 647 | * |
| 648 | * V2: |
| 649 | * ARGS[0]: FLAGS |
| 650 | */ |
| 651 | static int mbox_handle_close_window(struct mbox_context *context, |
| 652 | union mbox_regs *req, struct mbox_msg *resp) |
| 653 | { |
| 654 | uint8_t flags = 0; |
| 655 | int rc; |
| 656 | |
| 657 | /* Close the current window if there is one */ |
| 658 | if (context->current) { |
| 659 | /* There is an implicit flush if it was a write window */ |
| 660 | if (context->current_is_write) { |
| 661 | rc = mbox_handle_flush_window(context, NULL, NULL); |
| 662 | if (rc < 0) { |
| 663 | MSG_ERR("Couldn't Flush Write Window\n"); |
| 664 | return rc; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | if (context->version >= API_VERSION_2) { |
| 669 | flags = req->msg.args[0]; |
| 670 | } |
| 671 | |
| 672 | /* Host asked for it -> Don't set the BMC Event */ |
| 673 | close_current_window(context, NO_BMC_EVENT, flags); |
| 674 | } |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Command: BMC_EVENT_ACK |
| 681 | * Sent by the host to acknowledge BMC events supplied in mailbox register 15 |
| 682 | * |
| 683 | * ARGS[0]: Bitmap of bits to ack (by clearing) |
| 684 | */ |
| 685 | static int mbox_handle_ack(struct mbox_context *context, union mbox_regs *req, |
| 686 | struct mbox_msg *resp) |
| 687 | { |
| 688 | uint8_t bmc_events = req->msg.args[0]; |
| 689 | |
| 690 | return clr_bmc_events(context, (bmc_events & BMC_EVENT_ACK_MASK), |
| 691 | SET_BMC_EVENT); |
| 692 | } |
| 693 | |
| 694 | /* |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 695 | * check_req_valid() - Check if the given request is a valid mbox request |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 696 | * @context: The mbox context pointer |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 697 | * @cmd: The request registers |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 698 | * |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 699 | * Return: 0 if request is valid otherwise negative error code |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 700 | */ |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 701 | static int check_req_valid(struct mbox_context *context, union mbox_regs *req) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 702 | { |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 703 | uint8_t cmd = req->msg.command; |
| 704 | uint8_t seq = req->msg.seq; |
| 705 | |
| 706 | if (cmd > NUM_MBOX_CMDS) { |
Andrew Jeffery | 121dc0d | 2017-04-24 16:15:06 +0930 | [diff] [blame] | 707 | MSG_ERR("Unknown mbox command: %d\n", cmd); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 708 | return -MBOX_R_PARAM_ERROR; |
| 709 | } |
Andrew Jeffery | 121dc0d | 2017-04-24 16:15:06 +0930 | [diff] [blame] | 710 | |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 711 | if (seq == context->prev_seq && cmd != MBOX_C_GET_MBOX_INFO) { |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 712 | MSG_ERR("Invalid sequence number: %d, previous: %d\n", seq, |
| 713 | context->prev_seq); |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 714 | return -MBOX_R_SEQ_ERROR; |
| 715 | } |
| 716 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 717 | if (context->state & STATE_SUSPENDED) { |
| 718 | if (cmd != MBOX_C_GET_MBOX_INFO && cmd != MBOX_C_ACK) { |
| 719 | MSG_ERR("Cannot use that cmd while suspended: %d\n", |
| 720 | cmd); |
| 721 | return context->version >= API_VERSION_2 ? -MBOX_R_BUSY |
| 722 | : -MBOX_R_PARAM_ERROR; |
| 723 | } |
| 724 | } |
Andrew Jeffery | 121dc0d | 2017-04-24 16:15:06 +0930 | [diff] [blame] | 725 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 726 | if (!(context->state & MAPS_MEM)) { |
| 727 | if (cmd != MBOX_C_RESET_STATE && cmd != MBOX_C_GET_MBOX_INFO |
| 728 | && cmd != MBOX_C_ACK) { |
Andrew Jeffery | 121dc0d | 2017-04-24 16:15:06 +0930 | [diff] [blame] | 729 | MSG_ERR("Must call GET_MBOX_INFO before %d\n", cmd); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 730 | return -MBOX_R_PARAM_ERROR; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static const mboxd_mbox_handler mbox_handlers[] = { |
| 738 | mbox_handle_reset, |
| 739 | mbox_handle_mbox_info, |
| 740 | mbox_handle_flash_info, |
| 741 | mbox_handle_read_window, |
| 742 | mbox_handle_close_window, |
| 743 | mbox_handle_write_window, |
| 744 | mbox_handle_dirty_window, |
| 745 | mbox_handle_flush_window, |
| 746 | mbox_handle_ack, |
| 747 | mbox_handle_erase_window |
| 748 | }; |
| 749 | |
| 750 | /* |
| 751 | * handle_mbox_req() - Handle an incoming mbox command request |
| 752 | * @context: The mbox context pointer |
| 753 | * @req: The mbox request message |
| 754 | * |
| 755 | * Return: 0 if handled successfully otherwise negative error code |
| 756 | */ |
| 757 | static int handle_mbox_req(struct mbox_context *context, union mbox_regs *req) |
| 758 | { |
| 759 | struct mbox_msg resp = { |
| 760 | .command = req->msg.command, |
| 761 | .seq = req->msg.seq, |
| 762 | .args = { 0 }, |
| 763 | .response = MBOX_R_SUCCESS |
| 764 | }; |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 765 | int rc = 0, len, i; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 766 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 767 | MSG_INFO("Received MBOX command: %u\n", req->msg.command); |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 768 | rc = check_req_valid(context, req); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 769 | if (rc < 0) { |
| 770 | resp.response = -rc; |
| 771 | } else { |
| 772 | /* Commands start at 1 so we have to subtract 1 from the cmd */ |
| 773 | rc = mbox_handlers[req->msg.command - 1](context, req, &resp); |
| 774 | if (rc < 0) { |
| 775 | MSG_ERR("Error handling mbox cmd: %d\n", |
| 776 | req->msg.command); |
| 777 | resp.response = -rc; |
| 778 | } |
| 779 | } |
| 780 | |
Andrew Jeffery | 55dede6 | 2017-04-24 16:13:06 +0930 | [diff] [blame] | 781 | context->prev_seq = req->msg.seq; |
| 782 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 783 | MSG_DBG("Writing MBOX response:\n"); |
| 784 | MSG_DBG("MBOX cmd: %u\n", resp.command); |
| 785 | MSG_DBG("MBOX seq: %u\n", resp.seq); |
| 786 | for (i = 0; i < MBOX_ARGS_BYTES; i++) { |
| 787 | MSG_DBG("MBOX arg[%d]: 0x%.2x\n", i, resp.args[i]); |
| 788 | } |
| 789 | MSG_INFO("Writing MBOX response: %u\n", resp.response); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 790 | len = write(context->fds[MBOX_FD].fd, &resp, sizeof(resp)); |
| 791 | if (len < sizeof(resp)) { |
| 792 | MSG_ERR("Didn't write the full response\n"); |
| 793 | rc = -errno; |
| 794 | } |
| 795 | |
| 796 | return rc; |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * get_message() - Read an mbox request message from the mbox registers |
| 801 | * @context: The mbox context pointer |
| 802 | * @msg: Where to put the received message |
| 803 | * |
| 804 | * Return: 0 if read successfully otherwise negative error code |
| 805 | */ |
| 806 | static int get_message(struct mbox_context *context, union mbox_regs *msg) |
| 807 | { |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 808 | int rc, i; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 809 | |
| 810 | rc = read(context->fds[MBOX_FD].fd, msg, sizeof(msg->raw)); |
| 811 | if (rc < 0) { |
| 812 | MSG_ERR("Couldn't read: %s\n", strerror(errno)); |
| 813 | return -errno; |
| 814 | } else if (rc < sizeof(msg->raw)) { |
| 815 | MSG_ERR("Short read: %d expecting %zu\n", rc, sizeof(msg->raw)); |
| 816 | return -1; |
| 817 | } |
| 818 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 819 | MSG_DBG("Received MBOX request:\n"); |
| 820 | MSG_DBG("MBOX cmd: %u\n", msg->msg.command); |
| 821 | MSG_DBG("MBOX seq: %u\n", msg->msg.seq); |
| 822 | for (i = 0; i < MBOX_ARGS_BYTES; i++) { |
| 823 | MSG_DBG("MBOX arg[%d]: 0x%.2x\n", i, msg->msg.args[i]); |
| 824 | } |
| 825 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * dispatch_mbox() - handle an mbox interrupt |
| 831 | * @context: The mbox context pointer |
| 832 | * |
| 833 | * Return: 0 if handled successfully otherwise negative error code |
| 834 | */ |
| 835 | int dispatch_mbox(struct mbox_context *context) |
| 836 | { |
| 837 | int rc = 0; |
| 838 | union mbox_regs req = { 0 }; |
| 839 | |
| 840 | assert(context); |
| 841 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 842 | rc = get_message(context, &req); |
| 843 | if (rc) { |
| 844 | return rc; |
| 845 | } |
| 846 | |
| 847 | return handle_mbox_req(context, &req); |
| 848 | } |
| 849 | |
Andrew Jeffery | 913740f | 2017-04-10 23:51:07 +0930 | [diff] [blame] | 850 | int __init_mbox_dev(struct mbox_context *context, const char *path) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 851 | { |
| 852 | int fd; |
| 853 | |
| 854 | /* Open MBOX Device */ |
Andrew Jeffery | 913740f | 2017-04-10 23:51:07 +0930 | [diff] [blame] | 855 | fd = open(path, O_RDWR | O_NONBLOCK); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 856 | if (fd < 0) { |
| 857 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", |
Andrew Jeffery | 913740f | 2017-04-10 23:51:07 +0930 | [diff] [blame] | 858 | path, strerror(errno)); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 859 | return -errno; |
| 860 | } |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 861 | MSG_DBG("Opened mbox dev: %s\n", path); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 862 | |
| 863 | context->fds[MBOX_FD].fd = fd; |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
Andrew Jeffery | 913740f | 2017-04-10 23:51:07 +0930 | [diff] [blame] | 868 | int init_mbox_dev(struct mbox_context *context) |
| 869 | { |
| 870 | return __init_mbox_dev(context, MBOX_HOST_PATH); |
| 871 | } |
| 872 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 873 | void free_mbox_dev(struct mbox_context *context) |
| 874 | { |
| 875 | close(context->fds[MBOX_FD].fd); |
| 876 | } |