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 | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 6 | #include "common.h" |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 7 | #include "dbus.h" |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 8 | #include "mboxd.h" |
Andrew Jeffery | eebc6bd | 2018-08-08 10:38:19 +0930 | [diff] [blame] | 9 | #include "flash.h" |
Andrew Jeffery | cd18611 | 2018-08-08 10:47:55 +0930 | [diff] [blame] | 10 | #include "lpc.h" |
Andrew Jeffery | 457a6e5 | 2018-08-08 11:21:08 +0930 | [diff] [blame] | 11 | #include "transport_mbox.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 | { |
| 39 | int rc; |
| 40 | |
| 41 | /* We don't let the host access flash if the daemon is suspened */ |
| 42 | if (context->state & STATE_SUSPENDED) { |
| 43 | return -EBUSY; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * This will close (and flush) the current window and reset the lpc bus |
| 48 | * mapping back to flash, or memory in case we're using a virtual pnor. |
| 49 | * Better set the bmc event to notify the host of this. |
| 50 | */ |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 51 | if (windows_reset_all(context)) { |
| 52 | rc = protocol_events_set(context, BMC_EVENT_WINDOW_RESET); |
| 53 | if (rc < 0) { |
| 54 | return rc; |
| 55 | } |
| 56 | } |
Andrew Jeffery | 17971e4 | 2018-08-08 16:36:10 +0930 | [diff] [blame] | 57 | rc = lpc_reset(context); |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 58 | if (rc < 0) { |
| 59 | return rc; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int control_kill(struct mbox_context *context) |
| 66 | { |
| 67 | context->terminate = 1; |
| 68 | |
| 69 | MSG_INFO("DBUS Kill - Exiting...\n"); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | int control_modified(struct mbox_context *context) |
| 75 | { |
| 76 | /* Flash has been modified - can no longer trust our erased bytemap */ |
Andrew Jeffery | f953f79 | 2018-08-08 16:56:03 +0930 | [diff] [blame] | 77 | flash_set_bytemap(context, 0, context->flash_size, FLASH_DIRTY); |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 78 | |
| 79 | /* Force daemon to reload all windows -> Set BMC event to notify host */ |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 80 | if (windows_reset_all(context)) { |
| 81 | protocol_events_set(context, BMC_EVENT_WINDOW_RESET); |
| 82 | } |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int control_suspend(struct mbox_context *context) |
| 88 | { |
| 89 | int rc; |
| 90 | |
| 91 | if (context->state & STATE_SUSPENDED) { |
| 92 | /* Already Suspended */ |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 93 | return 0; |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Nothing to check - Just set the bit to notify the host */ |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 97 | rc = protocol_events_set(context, BMC_EVENT_FLASH_CTRL_LOST); |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 98 | if (rc < 0) { |
| 99 | return rc; |
| 100 | } |
| 101 | |
| 102 | context->state |= STATE_SUSPENDED; |
| 103 | |
| 104 | return rc; |
| 105 | } |
| 106 | |
| 107 | int control_resume(struct mbox_context *context, bool modified) |
| 108 | { |
| 109 | int rc; |
| 110 | |
| 111 | if (!(context->state & STATE_SUSPENDED)) { |
| 112 | /* We weren't suspended... */ |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 113 | return 0; |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (modified) { |
| 117 | /* Call the flash modified handler */ |
| 118 | control_modified(context); |
| 119 | } |
| 120 | |
| 121 | /* Clear the bit and send the BMC Event to the host */ |
Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 122 | rc = protocol_events_clear(context, BMC_EVENT_FLASH_CTRL_LOST); |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 123 | if (rc < 0) { |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 124 | return rc; |
Andrew Jeffery | 6802307 | 2018-08-06 10:08:11 +0930 | [diff] [blame] | 125 | } |
| 126 | context->state &= ~STATE_SUSPENDED; |
| 127 | |
| 128 | return rc; |
| 129 | } |