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