xref: /openbmc/hiomapd/control.c (revision 26558dbb00e439fb6ea18566361c0671ea6133f5)
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (C) 2018 IBM Corp.
3 #include <errno.h>
4 #include <stdlib.h>
5 
6 #include "common.h"
7 #include "dbus.h"
8 #include "mboxd.h"
9 #include "flash.h"
10 #include "lpc.h"
11 #include "transport_mbox.h"
12 #include "windows.h"
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 	 */
51 	windows_reset_all(context, EVENT_TRIGGER);
52 	rc = lpc_reset(context);
53 	if (rc < 0) {
54 		return rc;
55 	}
56 
57 	return 0;
58 }
59 
60 int control_kill(struct mbox_context *context)
61 {
62 	context->terminate = 1;
63 
64 	MSG_INFO("DBUS Kill - Exiting...\n");
65 
66 	return 0;
67 }
68 
69 int control_modified(struct mbox_context *context)
70 {
71 	/* Flash has been modified - can no longer trust our erased bytemap */
72 	flash_set_bytemap(context, 0, context->flash_size, FLASH_DIRTY);
73 
74 	/* Force daemon to reload all windows -> Set BMC event to notify host */
75 	windows_reset_all(context, EVENT_TRIGGER);
76 
77 	return 0;
78 }
79 
80 int control_suspend(struct mbox_context *context)
81 {
82 	int rc;
83 
84 	if (context->state & STATE_SUSPENDED) {
85 		/* Already Suspended */
86 		return 0;
87 	}
88 
89 	/* Nothing to check - Just set the bit to notify the host */
90 	rc = protocol_events_set(context, BMC_EVENT_FLASH_CTRL_LOST, EVENT_TRIGGER);
91 	if (rc < 0) {
92 		return rc;
93 	}
94 
95 	context->state |= STATE_SUSPENDED;
96 
97 	return rc;
98 }
99 
100 int control_resume(struct mbox_context *context, bool modified)
101 {
102 	int rc;
103 
104 	if (!(context->state & STATE_SUSPENDED)) {
105 		/* We weren't suspended... */
106 		return 0;
107 	}
108 
109 	if (modified) {
110 		/* Call the flash modified handler */
111 		control_modified(context);
112 	}
113 
114 	/* Clear the bit and send the BMC Event to the host */
115 	rc = protocol_events_clear(context, BMC_EVENT_FLASH_CTRL_LOST, EVENT_TRIGGER);
116 	if (rc < 0) {
117 		return rc;
118 	}
119 	context->state &= ~STATE_SUSPENDED;
120 
121 	return rc;
122 }
123