daemon: Remove the concept of triggering or suppressing events

Rather, trigger them (or not) as necessary in the relevant code-paths.
This ensures that any call to one of protocol_events_{set,clear}()
actually has a consequence that we can set about dealing with in the
transport layer.

Change-Id: If64733fa53ed9def0da8330c99cbe48327bab934
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/control.c b/control.c
index 5470e45..7eae29c 100644
--- a/control.c
+++ b/control.c
@@ -48,7 +48,12 @@
 	 * mapping back to flash, or memory in case we're using a virtual pnor.
 	 * Better set the bmc event to notify the host of this.
 	 */
-	windows_reset_all(context, EVENT_TRIGGER);
+	if (windows_reset_all(context)) {
+		rc = protocol_events_set(context, BMC_EVENT_WINDOW_RESET);
+		if (rc < 0) {
+			return rc;
+		}
+	}
 	rc = lpc_reset(context);
 	if (rc < 0) {
 		return rc;
@@ -72,7 +77,9 @@
 	flash_set_bytemap(context, 0, context->flash_size, FLASH_DIRTY);
 
 	/* Force daemon to reload all windows -> Set BMC event to notify host */
-	windows_reset_all(context, EVENT_TRIGGER);
+	if (windows_reset_all(context)) {
+		protocol_events_set(context, BMC_EVENT_WINDOW_RESET);
+	}
 
 	return 0;
 }
@@ -87,7 +94,7 @@
 	}
 
 	/* Nothing to check - Just set the bit to notify the host */
-	rc = protocol_events_set(context, BMC_EVENT_FLASH_CTRL_LOST, EVENT_TRIGGER);
+	rc = protocol_events_set(context, BMC_EVENT_FLASH_CTRL_LOST);
 	if (rc < 0) {
 		return rc;
 	}
@@ -112,7 +119,7 @@
 	}
 
 	/* Clear the bit and send the BMC Event to the host */
-	rc = protocol_events_clear(context, BMC_EVENT_FLASH_CTRL_LOST, EVENT_TRIGGER);
+	rc = protocol_events_clear(context, BMC_EVENT_FLASH_CTRL_LOST);
 	if (rc < 0) {
 		return rc;
 	}
diff --git a/mboxd.c b/mboxd.c
index dece231..86a67f3 100644
--- a/mboxd.c
+++ b/mboxd.c
@@ -147,7 +147,14 @@
 				break;
 			case SIGHUP:
 				/* Host didn't request reset -> Notify it */
-				windows_reset_all(context, EVENT_TRIGGER);
+				if (windows_reset_all(context)) {
+				       rc = protocol_events_set(context,
+						     BMC_EVENT_WINDOW_RESET);
+				       if (rc < 0) {
+					      MSG_ERR("Failed to notify host of reset, expect host-side corruption\n");
+					      break;
+				       }
+				}
 				rc = lpc_reset(context);
 				if (rc < 0) {
 					MSG_ERR("WARNING: Failed to point the "
@@ -185,7 +192,13 @@
 
 	/* Best to reset windows and the lpc mapping for safety */
 	/* Host didn't request reset -> Notify it */
-	windows_reset_all(context, EVENT_TRIGGER);
+	if (windows_reset_all(context)) {
+	       rc = protocol_events_set(context, BMC_EVENT_WINDOW_RESET);
+	       if (rc < 0) {
+		      MSG_ERR("Failed to notify host of reset, expect host-side corruption\n");
+		      return rc;
+	       }
+	}
 	rc = lpc_reset(context);
 	/* Not much we can do if this fails */
 	if (rc < 0) {
@@ -402,7 +415,7 @@
 		MSG_ERR("LPC configuration failed, RESET required: %d\n", rc);
 	}
 
-	rc = protocol_events_set(context, BMC_EVENT_DAEMON_READY, EVENT_TRIGGER);
+	rc = protocol_events_set(context, BMC_EVENT_DAEMON_READY);
 	if (rc) {
 		goto finish;
 	}
@@ -414,7 +427,7 @@
 
 finish:
 	MSG_INFO("Daemon Exiting...\n");
-	protocol_events_clear(context, BMC_EVENT_DAEMON_READY, EVENT_TRIGGER);
+	protocol_events_clear(context, BMC_EVENT_DAEMON_READY);
 
 #ifdef VIRTUAL_PNOR_ENABLED
 	destroy_vpnor(context);
diff --git a/protocol.c b/protocol.c
index c6934e6..58dad98 100644
--- a/protocol.c
+++ b/protocol.c
@@ -17,12 +17,10 @@
  * 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)
+int protocol_events_set(struct mbox_context *context, uint8_t bmc_event)
 {
 	uint8_t mask = 0x00;
 
@@ -38,30 +36,28 @@
 	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;
+	return context->transport->flush_events(context);
 }
 
 /*
  * 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)
+int protocol_events_clear(struct mbox_context *context, uint8_t bmc_event)
 {
 	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;
+	return context->transport->flush_events(context);
 }
 
 int protocol_v1_reset(struct mbox_context *context)
 {
 	/* Host requested it -> No BMC Event */
-	windows_reset_all(context, EVENT_SUPPRESS);
+	windows_reset_all(context);
 	return lpc_reset(context);
 }
 
@@ -78,7 +74,8 @@
 
 	/* Do the {up,down}grade if necessary*/
 	if (rc != old_version) {
-		windows_reset_all(context, EVENT_TRIGGER);
+		/* Doing version negotiation, don't alert host to reset */
+		windows_reset_all(context);
 		return context->protocol->get_info(context, io);
 	}
 
@@ -151,7 +148,7 @@
 				return rc;
 			}
 		}
-		windows_close_current(context, EVENT_SUPPRESS, FLAGS_NONE);
+		windows_close_current(context, FLAGS_NONE);
 	}
 
 	/* Offset the host has requested */
@@ -331,15 +328,15 @@
 	}
 
 	/* Host asked for it -> Don't set the BMC Event */
-	windows_close_current(context, EVENT_SUPPRESS, io->req.flags);
+	windows_close_current(context, io->req.flags);
 
 	return 0;
 }
 
 int protocol_v1_ack(struct mbox_context *context, struct protocol_ack *io)
 {
-	return protocol_events_clear(context, (io->req.flags & BMC_EVENT_ACK_MASK),
-			      EVENT_TRIGGER);
+	return protocol_events_clear(context,
+				     (io->req.flags & BMC_EVENT_ACK_MASK));
 }
 
 /*
@@ -374,7 +371,8 @@
 
 	/* Do the {up,down}grade if necessary*/
 	if (rc != old_version) {
-		windows_reset_all(context, EVENT_TRIGGER);
+		/* Doing version negotiation, don't alert host to reset */
+		windows_reset_all(context);
 		return context->protocol->get_info(context, io);
 	}
 
@@ -496,7 +494,7 @@
 	}
 
 	/* Host asked for it -> Don't set the BMC Event */
-	windows_close_current(context, EVENT_SUPPRESS, io->req.flags);
+	windows_close_current(context, io->req.flags);
 
 	return 0;
 }
diff --git a/protocol.h b/protocol.h
index f2b8e6f..44de89e 100644
--- a/protocol.h
+++ b/protocol.h
@@ -120,13 +120,8 @@
 
 int protocol_negotiate_version(struct mbox_context *context, uint8_t requested);
 
-#define EVENT_SUPPRESS		false
-#define EVENT_TRIGGER		true
-
-int protocol_events_set(struct mbox_context *context, uint8_t bmc_event,
-			bool write_back);
-int protocol_events_clear(struct mbox_context *context, uint8_t bmc_event,
-			  bool write_back);
+int protocol_events_set(struct mbox_context *context, uint8_t bmc_event);
+int protocol_events_clear(struct mbox_context *context, uint8_t bmc_event);
 
 /* Protocol v1 */
 int protocol_v1_reset(struct mbox_context *context);
diff --git a/test/bmc_event_ack_v2.c b/test/bmc_event_ack_v2.c
index 51f026e..719c27b 100644
--- a/test/bmc_event_ack_v2.c
+++ b/test/bmc_event_ack_v2.c
@@ -50,7 +50,7 @@
 	rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
 	assert(rc == 1);
 
-	protocol_events_set(ctx, FLAGS, EVENT_TRIGGER);
+	protocol_events_set(ctx, FLAGS);
 
 	rc = mbox_command_dispatch(ctx, command, sizeof(command));
 	assert(rc == 1);
diff --git a/windows.c b/windows.c
index 3d3104a..0b852c5 100644
--- a/windows.c
+++ b/windows.c
@@ -379,7 +379,6 @@
 /*
  * windows_close_current() - Close the current (active) window
  * @context:   		The mbox context pointer
- * @set_bmc_event:	Whether to set the bmc event bit
  * @flags:		Flags as defined for a close command in the protocol
  *
  * This closes the current window. If the host has requested the current window
@@ -388,15 +387,10 @@
  * without the host requesting it the bmc event bit must be set to indicate this
  * to the host (set_bmc_event == true).
  */
-void windows_close_current(struct mbox_context *context, bool set_bmc_event,
-			  uint8_t flags)
+void windows_close_current(struct mbox_context *context, uint8_t flags)
 {
 	MSG_DBG("Close current window, flags: 0x%.2x\n", flags);
 
-	if (set_bmc_event) {
-		protocol_events_set(context, BMC_EVENT_WINDOW_RESET, EVENT_TRIGGER);
-	}
-
 	if (flags & FLAGS_SHORT_LIFETIME) {
 		context->current->age = 0;
 	}
@@ -425,22 +419,29 @@
 /*
  * windows_reset_all() - Reset all windows to a well defined default state
  * @context:		The mbox context pointer
- * @set_bmc_event:	If any state change should be indicated to the host
+ *
+ * @return True if there was a window open that was closed, false otherwise
  */
-void windows_reset_all(struct mbox_context *context, bool set_bmc_event)
+bool windows_reset_all(struct mbox_context *context)
 {
+	bool closed = context->current;
 	int i;
 
 	MSG_DBG("Resetting all windows\n");
+
+	context->windows.max_age = 0;
+
 	/* We might have an open window which needs closing */
+
 	if (context->current) {
-		windows_close_current(context, set_bmc_event, FLAGS_NONE);
+		windows_close_current(context, FLAGS_NONE);
 	}
+
 	for (i = 0; i < context->windows.num; i++) {
 		window_reset(context, &context->windows.window[i]);
 	}
 
-	context->windows.max_age = 0;
+	return closed;
 }
 
 /*
diff --git a/windows.h b/windows.h
index 1e90066..10d76f9 100644
--- a/windows.h
+++ b/windows.h
@@ -43,10 +43,9 @@
 void windows_alloc_dirty_bytemap(struct mbox_context *context);
 int window_set_bytemap(struct mbox_context *context, struct window_context *cur,
 		       uint32_t offset, uint32_t size, uint8_t val);
-void windows_close_current(struct mbox_context *context, bool set_bmc_event,
-			  uint8_t flags);
+void windows_close_current(struct mbox_context *context, uint8_t flags);
 void window_reset(struct mbox_context *context, struct window_context *window);
-void windows_reset_all(struct mbox_context *context, bool set_bmc_event);
+bool windows_reset_all(struct mbox_context *context);
 struct window_context *windows_find_oldest(struct mbox_context *context);
 struct window_context *windows_find_largest(struct mbox_context *context);
 struct window_context *windows_search(struct mbox_context *context,