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