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); 115 116 return write_back ? write_bmc_event_reg(context) : 0; 117 } 118 119 /* 120 * clr_bmc_events() - Clear BMC events 121 * @context: The mbox context pointer 122 * @bmc_event: The bits to clear 123 * @write_back: Whether to write back to the register -> will interrupt host 124 * 125 * Return: 0 on success otherwise negative error code 126 */ 127 int clr_bmc_events(struct mbox_context *context, uint8_t bmc_event, 128 bool write_back) 129 { 130 context->bmc_events &= ~bmc_event; 131 132 return write_back ? write_bmc_event_reg(context) : 0; 133 } 134 135 /* Command Handlers */ 136 137 /* 138 * Command: RESET_STATE 139 * Reset the LPC mapping to point back at the flash 140 */ 141 static int mbox_handle_reset(struct mbox_context *context, 142 union mbox_regs *req, struct mbox_msg *resp) 143 { 144 /* Host requested it -> No BMC Event */ 145 reset_all_windows(context, NO_BMC_EVENT); 146 return point_to_flash(context); 147 } 148 149 /* 150 * Command: GET_MBOX_INFO 151 * Get the API version, default window size and block size 152 * We also set the LPC mapping to point to the reserved memory region here so 153 * this command must be called before any window manipulation 154 * 155 * V1: 156 * ARGS[0]: API Version 157 * 158 * RESP[0]: API Version 159 * RESP[1:2]: Default read window size (number of blocks) 160 * RESP[3:4]: Default write window size (number of blocks) 161 * RESP[5]: Block size (as shift) 162 * 163 * V2: 164 * ARGS[0]: API Version 165 * 166 * RESP[0]: API Version 167 * RESP[1:2]: Default read window size (number of blocks) 168 * RESP[3:4]: Default write window size (number of blocks) 169 * RESP[5]: Block size (as shift) 170 */ 171 static int mbox_handle_mbox_info(struct mbox_context *context, 172 union mbox_regs *req, struct mbox_msg *resp) 173 { 174 uint8_t mbox_api_version = req->msg.args[0]; 175 uint8_t old_api_version = context->version; 176 int rc; 177 178 /* Check we support the version requested */ 179 if (mbox_api_version < API_MIN_VERSION) 180 return -MBOX_R_PARAM_ERROR; 181 182 if (mbox_api_version > API_MAX_VERSION) 183 mbox_api_version = API_MAX_VERSION; 184 185 context->version = mbox_api_version; 186 MSG_OUT("Using Protocol Version: %d\n", context->version); 187 188 /* 189 * The reset state is currently to have the LPC bus point directly to 190 * flash, since we got a mbox_info command we know the host can talk 191 * mbox so point the LPC bus mapping to the reserved memory region now 192 * so the host can access what we put in it. 193 */ 194 rc = point_to_memory(context); 195 if (rc < 0) { 196 return rc; 197 } 198 199 switch (context->version) { 200 case API_VERSION_1: 201 context->block_size_shift = BLOCK_SIZE_SHIFT_V1; 202 break; 203 default: 204 context->block_size_shift = log_2(context->mtd_info.erasesize); 205 break; 206 } 207 MSG_OUT("Block Size Shift: %d\n", context->block_size_shift); 208 209 /* Now we know the blocksize we can allocate the window dirty_bytemap */ 210 if (mbox_api_version != old_api_version) { 211 alloc_window_dirty_bytemap(context); 212 } 213 /* Reset if we were V1 since this required exact window mapping */ 214 if (old_api_version == API_VERSION_1) { 215 /* 216 * This will only set the BMC event if there was a current 217 * window -> In which case we are better off notifying the 218 * host. 219 */ 220 reset_all_windows(context, SET_BMC_EVENT); 221 } 222 223 resp->args[0] = mbox_api_version; 224 if (context->version == API_VERSION_1) { 225 put_u16(&resp->args[1], context->windows.default_size >> 226 context->block_size_shift); 227 put_u16(&resp->args[3], context->windows.default_size >> 228 context->block_size_shift); 229 } 230 if (context->version >= API_VERSION_2) { 231 resp->args[5] = context->block_size_shift; 232 } 233 234 return 0; 235 } 236 237 /* 238 * Command: GET_FLASH_INFO 239 * Get the flash size and erase granularity 240 * 241 * V1: 242 * RESP[0:3]: Flash Size (bytes) 243 * RESP[4:7]: Erase Size (bytes) 244 * V2: 245 * RESP[0:1]: Flash Size (number of blocks) 246 * RESP[2:3]: Erase Size (number of blocks) 247 */ 248 static int mbox_handle_flash_info(struct mbox_context *context, 249 union mbox_regs *req, struct mbox_msg *resp) 250 { 251 switch (context->version) { 252 case API_VERSION_1: 253 /* Both Sizes in Bytes */ 254 put_u32(&resp->args[0], context->flash_size); 255 put_u32(&resp->args[4], context->mtd_info.erasesize); 256 break; 257 case API_VERSION_2: 258 /* Both Sizes in Block Size */ 259 put_u16(&resp->args[0], 260 context->flash_size >> context->block_size_shift); 261 put_u16(&resp->args[2], 262 context->mtd_info.erasesize >> 263 context->block_size_shift); 264 break; 265 default: 266 MSG_ERR("API Version Not Valid - Invalid System State\n"); 267 return -MBOX_R_SYSTEM_ERROR; 268 } 269 270 return 0; 271 } 272 273 /* 274 * get_lpc_addr_shifted() - Get lpc address of the current window 275 * @context: The mbox context pointer 276 * 277 * Return: The lpc address to access that offset shifted by block size 278 */ 279 static inline uint16_t get_lpc_addr_shifted(struct mbox_context *context) 280 { 281 uint32_t lpc_addr, mem_offset; 282 283 /* Offset of the current window in the reserved memory region */ 284 mem_offset = context->current->mem - context->mem; 285 /* Total LPC Address */ 286 lpc_addr = context->lpc_base + mem_offset; 287 288 return lpc_addr >> context->block_size_shift; 289 } 290 291 /* 292 * Command: CREATE_READ_WINDOW 293 * Opens a read window 294 * First checks if any current window with the requested data, if so we just 295 * point the host to that. Otherwise we read the request data in from flash and 296 * point the host there. 297 * 298 * V1: 299 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks) 300 * 301 * RESP[0:1]: LPC bus address for host to access this window (number of blocks) 302 * 303 * V2: 304 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks) 305 * ARGS[2:3]: Requested window size (number of blocks) 306 * 307 * RESP[0:1]: LPC bus address for host to access this window (number of blocks) 308 * RESP[2:3]: Actual window size that the host can access (number of blocks) 309 */ 310 static int mbox_handle_read_window(struct mbox_context *context, 311 union mbox_regs *req, struct mbox_msg *resp) 312 { 313 uint32_t flash_offset; 314 int rc; 315 316 /* Close the current window if there is one */ 317 if (context->current) { 318 /* There is an implicit flush if it was a write window */ 319 if (context->current_is_write) { 320 rc = mbox_handle_flush_window(context, NULL, NULL); 321 if (rc < 0) { 322 MSG_ERR("Couldn't Flush Write Window\n"); 323 return rc; 324 } 325 } 326 close_current_window(context, NO_BMC_EVENT, FLAGS_NONE); 327 } 328 329 /* Offset the host has requested */ 330 flash_offset = get_u16(&req->msg.args[0]) << context->block_size_shift; 331 MSG_OUT("Host Requested Flash @ 0x%.8x\n", flash_offset); 332 /* Check if we have an existing window */ 333 context->current = search_windows(context, flash_offset, 334 context->version == API_VERSION_1); 335 336 if (!context->current) { /* No existing window */ 337 rc = create_map_window(context, &context->current, flash_offset, 338 context->version == API_VERSION_1); 339 if (rc < 0) { /* Unable to map offset */ 340 MSG_ERR("Couldn't create window mapping for offset 0x%.8x\n" 341 , flash_offset); 342 return rc; 343 } 344 } 345 346 put_u16(&resp->args[0], get_lpc_addr_shifted(context)); 347 if (context->version >= API_VERSION_2) { 348 put_u16(&resp->args[2], context->current->size >> 349 context->block_size_shift); 350 put_u16(&resp->args[4], context->current->flash_offset >> 351 context->block_size_shift); 352 } 353 354 context->current_is_write = false; 355 356 return 0; 357 } 358 359 /* 360 * Command: CREATE_WRITE_WINDOW 361 * Opens a write window 362 * First checks if any current window with the requested data, if so we just 363 * point the host to that. Otherwise we read the request data in from flash and 364 * point the host there. 365 * 366 * V1: 367 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks) 368 * 369 * RESP[0:1]: LPC bus address for host to access this window (number of blocks) 370 * 371 * V2: 372 * ARGS[0:1]: Window Location as Offset into Flash (number of blocks) 373 * ARGS[2:3]: Requested window size (number of blocks) 374 * 375 * RESP[0:1]: LPC bus address for host to access this window (number of blocks) 376 * RESP[2:3]: Actual window size that was mapped/host can access (n.o. blocks) 377 */ 378 static int mbox_handle_write_window(struct mbox_context *context, 379 union mbox_regs *req, struct mbox_msg *resp) 380 { 381 int rc; 382 383 /* 384 * This is very similar to opening a read window (exactly the same 385 * for now infact) 386 */ 387 rc = mbox_handle_read_window(context, req, resp); 388 if (rc < 0) { 389 return rc; 390 } 391 392 context->current_is_write = true; 393 return rc; 394 } 395 396 /* 397 * Commands: MARK_WRITE_DIRTY 398 * Marks a portion of the current (write) window dirty, informing the daemon 399 * that is has been written to and thus must be at some point written to the 400 * backing store 401 * These changes aren't written back to the backing store unless flush is then 402 * called or the window closed 403 * 404 * V1: 405 * ARGS[0:1]: Where within flash to start (number of blocks) 406 * ARGS[2:5]: Number to mark dirty (number of bytes) 407 * 408 * V2: 409 * ARGS[0:1]: Where within window to start (number of blocks) 410 * ARGS[2:3]: Number to mark dirty (number of blocks) 411 */ 412 static int mbox_handle_dirty_window(struct mbox_context *context, 413 union mbox_regs *req, struct mbox_msg *resp) 414 { 415 uint32_t offset, size; 416 417 if (!(context->current && context->current_is_write)) { 418 MSG_ERR("Tried to call mark dirty without open write window\n"); 419 return context->version >= API_VERSION_2 ? -MBOX_R_WINDOW_ERROR 420 : -MBOX_R_PARAM_ERROR; 421 } 422 423 offset = get_u16(&req->msg.args[0]); 424 425 if (context->version >= API_VERSION_2) { 426 size = get_u16(&req->msg.args[2]); 427 } else { 428 uint32_t off; 429 /* For V1 offset given relative to flash - we want the window */ 430 off = offset - ((context->current->flash_offset) >> 431 context->block_size_shift); 432 if (off > offset) { /* Underflow - before current window */ 433 MSG_ERR("Tried to mark dirty before start of window\n"); 434 MSG_ERR("requested offset: 0x%x window start: 0x%x\n", 435 offset << context->block_size_shift, 436 context->current->flash_offset); 437 return -MBOX_R_PARAM_ERROR; 438 } 439 offset = off; 440 size = get_u32(&req->msg.args[2]); 441 /* 442 * We only track dirty at the block level. 443 * For protocol V1 we can get away with just marking the whole 444 * block dirty. 445 */ 446 size = align_up(size, 1 << context->block_size_shift); 447 size >>= context->block_size_shift; 448 } 449 450 return set_window_bytemap(context, context->current, offset, size, 451 WINDOW_DIRTY); 452 } 453 454 /* 455 * Commands: MARK_WRITE_ERASE 456 * Erases a portion of the current window 457 * These changes aren't written back to the backing store unless flush is then 458 * called or the window closed 459 * 460 * V1: 461 * Unimplemented 462 * 463 * V2: 464 * ARGS[0:1]: Where within window to start (number of blocks) 465 * ARGS[2:3]: Number to erase (number of blocks) 466 */ 467 static int mbox_handle_erase_window(struct mbox_context *context, 468 union mbox_regs *req, struct mbox_msg *resp) 469 { 470 uint32_t offset, size; 471 int rc; 472 473 if (context->version < API_VERSION_2) { 474 MSG_ERR("Protocol Version invalid for Erase Command\n"); 475 return -MBOX_R_PARAM_ERROR; 476 } 477 478 if (!(context->current && context->current_is_write)) { 479 MSG_ERR("Tried to call erase without open write window\n"); 480 return -MBOX_R_WINDOW_ERROR; 481 } 482 483 offset = get_u16(&req->msg.args[0]); 484 size = get_u16(&req->msg.args[2]); 485 486 rc = set_window_bytemap(context, context->current, offset, size, 487 WINDOW_ERASED); 488 if (rc < 0) { 489 return rc; 490 } 491 492 /* Write 0xFF to mem -> This ensures consistency between flash & ram */ 493 memset(context->current->mem + (offset << context->block_size_shift), 494 0xFF, size << context->block_size_shift); 495 496 return 0; 497 } 498 499 /* 500 * Command: WRITE_FLUSH 501 * Flushes any dirty or erased blocks in the current window back to the backing 502 * store 503 * NOTE: For V1 this behaves much the same as the dirty command in that it 504 * takes an offset and number of blocks to dirty, then also performs a flush as 505 * part of the same command. For V2 this will only flush blocks already marked 506 * dirty/erased with the appropriate commands and doesn't take any arguments 507 * directly. 508 * 509 * V1: 510 * ARGS[0:1]: Where within window to start (number of blocks) 511 * ARGS[2:5]: Number to mark dirty (number of bytes) 512 * 513 * V2: 514 * NONE 515 */ 516 static int mbox_handle_flush_window(struct mbox_context *context, 517 union mbox_regs *req, struct mbox_msg *resp) 518 { 519 int rc, i, offset, count; 520 uint8_t prev; 521 522 if (!(context->current && context->current_is_write)) { 523 MSG_ERR("Tried to call flush without open write window\n"); 524 return context->version >= API_VERSION_2 ? -MBOX_R_WINDOW_ERROR 525 : -MBOX_R_PARAM_ERROR; 526 } 527 528 /* 529 * For V1 the Flush command acts much the same as the dirty command 530 * except with a flush as well. Only do this on an actual flush 531 * command not when we call flush because we've implicitly closed a 532 * window because we might not have the required args in req. 533 */ 534 if (context->version == API_VERSION_1 && req && 535 req->msg.command == MBOX_C_WRITE_FLUSH) { 536 rc = mbox_handle_dirty_window(context, req, NULL); 537 if (rc < 0) { 538 return rc; 539 } 540 } 541 542 offset = 0; 543 count = 0; 544 prev = WINDOW_CLEAN; 545 546 /* 547 * We look for streaks of the same type and keep a count, when the type 548 * (dirty/erased) changes we perform the required action on the backing 549 * store and update the current streak-type 550 */ 551 for (i = 0; i < (context->current->size >> context->block_size_shift); 552 i++) { 553 uint8_t cur = context->current->dirty_bmap[i]; 554 if (cur != WINDOW_CLEAN) { 555 if (cur == prev) { /* Same as previous block, incrmnt */ 556 count++; 557 } else if (prev == WINDOW_CLEAN) { /* Start of run */ 558 offset = i; 559 count++; 560 } else { /* Change in streak type */ 561 rc = write_from_window(context, offset, count, 562 prev); 563 if (rc < 0) { 564 return rc; 565 } 566 offset = i; 567 count = 1; 568 } 569 } else { 570 if (prev != WINDOW_CLEAN) { /* End of a streak */ 571 rc = write_from_window(context, offset, count, 572 prev); 573 if (rc < 0) { 574 return rc; 575 } 576 offset = 0; 577 count = 0; 578 } 579 } 580 prev = cur; 581 } 582 583 if (prev != WINDOW_CLEAN) { /* Still the last streak to write */ 584 rc = write_from_window(context, offset, count, prev); 585 if (rc < 0) { 586 return rc; 587 } 588 } 589 590 /* Clear the dirty bytemap since we have written back all changes */ 591 return set_window_bytemap(context, context->current, 0, 592 context->current->size >> 593 context->block_size_shift, 594 WINDOW_CLEAN); 595 } 596 597 /* 598 * Command: CLOSE_WINDOW 599 * Close the current window 600 * NOTE: There is an implicit flush 601 * 602 * V1: 603 * NONE 604 * 605 * V2: 606 * ARGS[0]: FLAGS 607 */ 608 static int mbox_handle_close_window(struct mbox_context *context, 609 union mbox_regs *req, struct mbox_msg *resp) 610 { 611 uint8_t flags = 0; 612 int rc; 613 614 /* Close the current window if there is one */ 615 if (context->current) { 616 /* There is an implicit flush if it was a write window */ 617 if (context->current_is_write) { 618 rc = mbox_handle_flush_window(context, NULL, NULL); 619 if (rc < 0) { 620 MSG_ERR("Couldn't Flush Write Window\n"); 621 return rc; 622 } 623 } 624 625 if (context->version >= API_VERSION_2) { 626 flags = req->msg.args[0]; 627 } 628 629 /* Host asked for it -> Don't set the BMC Event */ 630 close_current_window(context, NO_BMC_EVENT, flags); 631 } 632 633 return 0; 634 } 635 636 /* 637 * Command: BMC_EVENT_ACK 638 * Sent by the host to acknowledge BMC events supplied in mailbox register 15 639 * 640 * ARGS[0]: Bitmap of bits to ack (by clearing) 641 */ 642 static int mbox_handle_ack(struct mbox_context *context, union mbox_regs *req, 643 struct mbox_msg *resp) 644 { 645 uint8_t bmc_events = req->msg.args[0]; 646 647 return clr_bmc_events(context, (bmc_events & BMC_EVENT_ACK_MASK), 648 SET_BMC_EVENT); 649 } 650 651 /* 652 * check_req_valid() - Check if the given request is a valid mbox request 653 * @context: The mbox context pointer 654 * @cmd: The request registers 655 * 656 * Return: 0 if request is valid otherwise negative error code 657 */ 658 static int check_req_valid(struct mbox_context *context, union mbox_regs *req) 659 { 660 uint8_t cmd = req->msg.command; 661 uint8_t seq = req->msg.seq; 662 663 if (cmd > NUM_MBOX_CMDS) { 664 MSG_ERR("Unknown mbox command: %d\n", cmd); 665 return -MBOX_R_PARAM_ERROR; 666 } 667 668 if (seq == context->prev_seq && cmd != MBOX_C_GET_MBOX_INFO) { 669 MSG_ERR("Invalid sequence number: %d\n", seq); 670 return -MBOX_R_SEQ_ERROR; 671 } 672 673 if (context->state & STATE_SUSPENDED) { 674 if (cmd != MBOX_C_GET_MBOX_INFO && cmd != MBOX_C_ACK) { 675 MSG_ERR("Cannot use that cmd while suspended: %d\n", 676 cmd); 677 return context->version >= API_VERSION_2 ? -MBOX_R_BUSY 678 : -MBOX_R_PARAM_ERROR; 679 } 680 } 681 682 if (!(context->state & MAPS_MEM)) { 683 if (cmd != MBOX_C_RESET_STATE && cmd != MBOX_C_GET_MBOX_INFO 684 && cmd != MBOX_C_ACK) { 685 MSG_ERR("Must call GET_MBOX_INFO before %d\n", cmd); 686 return -MBOX_R_PARAM_ERROR; 687 } 688 } 689 690 return 0; 691 } 692 693 static const mboxd_mbox_handler mbox_handlers[] = { 694 mbox_handle_reset, 695 mbox_handle_mbox_info, 696 mbox_handle_flash_info, 697 mbox_handle_read_window, 698 mbox_handle_close_window, 699 mbox_handle_write_window, 700 mbox_handle_dirty_window, 701 mbox_handle_flush_window, 702 mbox_handle_ack, 703 mbox_handle_erase_window 704 }; 705 706 /* 707 * handle_mbox_req() - Handle an incoming mbox command request 708 * @context: The mbox context pointer 709 * @req: The mbox request message 710 * 711 * Return: 0 if handled successfully otherwise negative error code 712 */ 713 static int handle_mbox_req(struct mbox_context *context, union mbox_regs *req) 714 { 715 struct mbox_msg resp = { 716 .command = req->msg.command, 717 .seq = req->msg.seq, 718 .args = { 0 }, 719 .response = MBOX_R_SUCCESS 720 }; 721 int rc = 0, len; 722 723 MSG_OUT("Got data in with command %d\n", req->msg.command); 724 rc = check_req_valid(context, req); 725 if (rc < 0) { 726 resp.response = -rc; 727 } else { 728 /* Commands start at 1 so we have to subtract 1 from the cmd */ 729 rc = mbox_handlers[req->msg.command - 1](context, req, &resp); 730 if (rc < 0) { 731 MSG_ERR("Error handling mbox cmd: %d\n", 732 req->msg.command); 733 resp.response = -rc; 734 } 735 } 736 737 context->prev_seq = req->msg.seq; 738 739 MSG_OUT("Writing response to MBOX regs: %d\n", resp.response); 740 len = write(context->fds[MBOX_FD].fd, &resp, sizeof(resp)); 741 if (len < sizeof(resp)) { 742 MSG_ERR("Didn't write the full response\n"); 743 rc = -errno; 744 } 745 746 return rc; 747 } 748 749 /* 750 * get_message() - Read an mbox request message from the mbox registers 751 * @context: The mbox context pointer 752 * @msg: Where to put the received message 753 * 754 * Return: 0 if read successfully otherwise negative error code 755 */ 756 static int get_message(struct mbox_context *context, union mbox_regs *msg) 757 { 758 int rc; 759 760 rc = read(context->fds[MBOX_FD].fd, msg, sizeof(msg->raw)); 761 if (rc < 0) { 762 MSG_ERR("Couldn't read: %s\n", strerror(errno)); 763 return -errno; 764 } else if (rc < sizeof(msg->raw)) { 765 MSG_ERR("Short read: %d expecting %zu\n", rc, sizeof(msg->raw)); 766 return -1; 767 } 768 769 return 0; 770 } 771 772 /* 773 * dispatch_mbox() - handle an mbox interrupt 774 * @context: The mbox context pointer 775 * 776 * Return: 0 if handled successfully otherwise negative error code 777 */ 778 int dispatch_mbox(struct mbox_context *context) 779 { 780 int rc = 0; 781 union mbox_regs req = { 0 }; 782 783 assert(context); 784 785 MSG_OUT("Dispatched to mbox\n"); 786 rc = get_message(context, &req); 787 if (rc) { 788 return rc; 789 } 790 791 return handle_mbox_req(context, &req); 792 } 793 794 int __init_mbox_dev(struct mbox_context *context, const char *path) 795 { 796 int fd; 797 798 /* Open MBOX Device */ 799 fd = open(path, O_RDWR | O_NONBLOCK); 800 if (fd < 0) { 801 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", 802 path, strerror(errno)); 803 return -errno; 804 } 805 806 context->fds[MBOX_FD].fd = fd; 807 808 return 0; 809 } 810 811 int init_mbox_dev(struct mbox_context *context) 812 { 813 return __init_mbox_dev(context, MBOX_HOST_PATH); 814 } 815 816 void free_mbox_dev(struct mbox_context *context) 817 { 818 close(context->fds[MBOX_FD].fd); 819 } 820