Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
| 3 | #include <errno.h> |
| 4 | #include <stdlib.h> |
| 5 | |
Andrew Jeffery | 5320f6e | 2019-03-15 12:40:41 +1030 | [diff] [blame] | 6 | #include "backend.h" |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 7 | #include "common.h" |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 8 | #include "dbus.h" |
Andrew Jeffery | cd18611 | 2018-08-08 10:47:55 +0930 | [diff] [blame] | 9 | #include "lpc.h" |
Andrew Jeffery | 5320f6e | 2019-03-15 12:40:41 +1030 | [diff] [blame] | 10 | #include "mboxd.h" |
| 11 | #include "protocol.h" |
Andrew Jeffery | f593b1b | 2018-08-08 11:01:04 +0930 | [diff] [blame] | 12 | #include "windows.h" |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 13 | |
| 14 | int control_ping(struct mbox_context *context) |
| 15 | { |
| 16 | return 0; |
| 17 | } |
| 18 | |
| 19 | int control_daemon_state(struct mbox_context *context) |
| 20 | { |
| 21 | return (context->state & STATE_SUSPENDED) ? |
| 22 | DAEMON_STATE_SUSPENDED : DAEMON_STATE_ACTIVE; |
| 23 | } |
| 24 | |
| 25 | int control_lpc_state(struct mbox_context *context) |
| 26 | { |
| 27 | if ((context->state & MAPS_MEM) && !(context->state & MAPS_FLASH)) { |
| 28 | return LPC_STATE_MEM; |
| 29 | } else if (!(context->state & MAPS_MEM) && |
| 30 | (context->state & MAPS_FLASH)) { |
| 31 | return LPC_STATE_FLASH; |
| 32 | } |
| 33 | |
| 34 | return LPC_STATE_INVALID; |
| 35 | } |
| 36 | |
| 37 | int control_reset(struct mbox_context *context) |
| 38 | { |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 39 | /* We don't let the host access flash if the daemon is suspened */ |
| 40 | if (context->state & STATE_SUSPENDED) { |
| 41 | return -EBUSY; |
| 42 | } |
| 43 | |
Andrew Jeffery | cda2964 | 2018-08-30 12:01:40 +0930 | [diff] [blame] | 44 | /* FIXME: Comment below is wrong: windows_reset_all() does not flush! */ |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 45 | /* |
| 46 | * This will close (and flush) the current window and reset the lpc bus |
| 47 | * mapping back to flash, or memory in case we're using a virtual pnor. |
| 48 | * Better set the bmc event to notify the host of this. |
| 49 | */ |
Andrew Jeffery | f69760d | 2019-03-14 16:54:13 +1030 | [diff] [blame] | 50 | return protocol_reset(context); |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int control_kill(struct mbox_context *context) |
| 54 | { |
| 55 | context->terminate = 1; |
| 56 | |
| 57 | MSG_INFO("DBUS Kill - Exiting...\n"); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int control_modified(struct mbox_context *context) |
| 63 | { |
| 64 | /* Flash has been modified - can no longer trust our erased bytemap */ |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 65 | backend_set_bytemap(&context->backend, 0, context->backend.flash_size, |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 66 | FLASH_DIRTY); |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 67 | |
| 68 | /* Force daemon to reload all windows -> Set BMC event to notify host */ |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 69 | if (windows_reset_all(context)) { |
| 70 | protocol_events_set(context, BMC_EVENT_WINDOW_RESET); |
| 71 | } |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int control_suspend(struct mbox_context *context) |
| 77 | { |
| 78 | int rc; |
| 79 | |
| 80 | if (context->state & STATE_SUSPENDED) { |
| 81 | /* Already Suspended */ |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 82 | return 0; |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* Nothing to check - Just set the bit to notify the host */ |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 86 | rc = protocol_events_set(context, BMC_EVENT_FLASH_CTRL_LOST); |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 87 | if (rc < 0) { |
| 88 | return rc; |
| 89 | } |
| 90 | |
| 91 | context->state |= STATE_SUSPENDED; |
| 92 | |
| 93 | return rc; |
| 94 | } |
| 95 | |
| 96 | int control_resume(struct mbox_context *context, bool modified) |
| 97 | { |
| 98 | int rc; |
| 99 | |
| 100 | if (!(context->state & STATE_SUSPENDED)) { |
| 101 | /* We weren't suspended... */ |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 102 | return 0; |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (modified) { |
| 106 | /* Call the flash modified handler */ |
| 107 | control_modified(context); |
| 108 | } |
| 109 | |
| 110 | /* Clear the bit and send the BMC Event to the host */ |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 111 | rc = protocol_events_clear(context, BMC_EVENT_FLASH_CTRL_LOST); |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 112 | if (rc < 0) { |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 113 | return rc; |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 114 | } |
| 115 | context->state &= ~STATE_SUSPENDED; |
| 116 | |
| 117 | return rc; |
| 118 | } |
Andrew Jeffery | 5320f6e | 2019-03-15 12:40:41 +1030 | [diff] [blame] | 119 | |
| 120 | int control_set_backend(struct mbox_context *context, struct backend *backend, |
| 121 | void *data) |
| 122 | { |
| 123 | int rc; |
| 124 | |
| 125 | if (context->state & STATE_SUSPENDED) |
| 126 | return -EINVAL; |
| 127 | |
| 128 | rc = protocol_events_clear(context, BMC_EVENT_DAEMON_READY); |
| 129 | if (rc < 0) |
| 130 | return rc; |
| 131 | |
| 132 | backend_free(&context->backend); |
| 133 | |
| 134 | rc = backend_init(&context->backend, backend, data); |
| 135 | if (rc < 0) |
| 136 | return rc; |
| 137 | |
| 138 | rc = __protocol_reset(context); |
| 139 | if (rc < 0) |
| 140 | return rc; |
| 141 | |
| 142 | return protocol_events_set(context, |
| 143 | BMC_EVENT_DAEMON_READY | BMC_EVENT_PROTOCOL_RESET); |
| 144 | } |