protocol: Provide abstraction over event notification

How this works will be transport-dependent. Move the event notification
helpers into the protocol abstraction and call-back through the
registered flush handler as necessary.

Change-Id: I29e3a9a9785b92de46a2b2750257fb7f8480a184
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/protocol.c b/protocol.c
index 4160e6e..c18083f 100644
--- a/protocol.c
+++ b/protocol.c
@@ -5,16 +5,61 @@
 #include <errno.h>
 #include <stdint.h>
 
+#include "common.h"
 #include "flash.h"
 #include "mbox.h"
 #include "lpc.h"
-#include "transport_mbox.h" /* TODO: Remove dependency on transport_mbox.h */
 #include "windows.h"
 
+/*
+ * protocol_events_set() - Set BMC events
+ * @context:	The mbox context pointer
+ * @bmc_event:	The bits to set
+ * @write_back:	Whether to write back to the register -> will interrupt host
+ *
+ * Return:	0 on success otherwise negative error code
+ */
+int protocol_events_set(struct mbox_context *context, uint8_t bmc_event,
+		   bool write_back)
+{
+	uint8_t mask = 0x00;
+
+	switch (context->version) {
+	case API_VERSION_1:
+		mask = BMC_EVENT_V1_MASK;
+		break;
+	default:
+		mask = BMC_EVENT_V2_MASK;
+		break;
+	}
+
+	context->bmc_events |= (bmc_event & mask);
+	MSG_DBG("BMC Events set to: 0x%.2x\n", context->bmc_events);
+
+	return write_back ? context->transport->flush_events(context) : 0;
+}
+
+/*
+ * protocol_events_clear() - Clear BMC events
+ * @context:	The mbox context pointer
+ * @bmc_event:	The bits to clear
+ * @write_back:	Whether to write back to the register -> will interrupt host
+ *
+ * Return:	0 on success otherwise negative error code
+ */
+int protocol_events_clear(struct mbox_context *context, uint8_t bmc_event,
+		   bool write_back)
+{
+	context->bmc_events &= ~bmc_event;
+	MSG_DBG("BMC Events clear to: 0x%.2x\n", context->bmc_events);
+
+	return write_back ? context->transport->flush_events(context) : 0;
+}
+
 int protocol_v1_reset(struct mbox_context *context)
 {
 	/* Host requested it -> No BMC Event */
-	windows_reset_all(context, NO_BMC_EVENT);
+	windows_reset_all(context, EVENT_SUPPRESS);
 	return lpc_reset(context);
 }
 
@@ -31,7 +76,7 @@
 
 	/* Do the {up,down}grade if necessary*/
 	if (rc != old_version) {
-		windows_reset_all(context, SET_BMC_EVENT);
+		windows_reset_all(context, EVENT_TRIGGER);
 		return context->protocol->get_info(context, io);
 	}
 
@@ -104,7 +149,7 @@
 				return rc;
 			}
 		}
-		windows_close_current(context, NO_BMC_EVENT, FLAGS_NONE);
+		windows_close_current(context, EVENT_SUPPRESS, FLAGS_NONE);
 	}
 
 	/* Offset the host has requested */
@@ -284,15 +329,15 @@
 	}
 
 	/* Host asked for it -> Don't set the BMC Event */
-	windows_close_current(context, NO_BMC_EVENT, io->req.flags);
+	windows_close_current(context, EVENT_SUPPRESS, io->req.flags);
 
 	return 0;
 }
 
 int protocol_v1_ack(struct mbox_context *context, struct protocol_ack *io)
 {
-	return clr_bmc_events(context, (io->req.flags & BMC_EVENT_ACK_MASK),
-			      SET_BMC_EVENT);
+	return protocol_events_clear(context, (io->req.flags & BMC_EVENT_ACK_MASK),
+			      EVENT_TRIGGER);
 }
 
 /*
@@ -327,7 +372,7 @@
 
 	/* Do the {up,down}grade if necessary*/
 	if (rc != old_version) {
-		windows_reset_all(context, SET_BMC_EVENT);
+		windows_reset_all(context, EVENT_TRIGGER);
 		return context->protocol->get_info(context, io);
 	}
 
@@ -449,7 +494,7 @@
 	}
 
 	/* Host asked for it -> Don't set the BMC Event */
-	windows_close_current(context, NO_BMC_EVENT, io->req.flags);
+	windows_close_current(context, EVENT_SUPPRESS, io->req.flags);
 
 	return 0;
 }