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