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