mbox: Clarify sequence number constraints

And implement the specified behaviour.

Change-Id: I268d5896aa8dda3875cd79f4ff18929c8e3aea49
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/mboxd_msg.c b/mboxd_msg.c
index 9f8aba1..a5bcf62 100644
--- a/mboxd_msg.c
+++ b/mboxd_msg.c
@@ -650,19 +650,27 @@
 }
 
 /*
- * check_cmd_valid() - Check if the given command is a valid mbox command code
+ * check_req_valid() - Check if the given request is a valid mbox request
  * @context:	The mbox context pointer
- * @cmd:	The command code
+ * @cmd:	The request registers
  *
- * Return:	0 if command is valid otherwise negative error code
+ * Return:	0 if request is valid otherwise negative error code
  */
-static int check_cmd_valid(struct mbox_context *context, int cmd)
+static int check_req_valid(struct mbox_context *context, union mbox_regs *req)
 {
-	if (cmd <= 0 || cmd > NUM_MBOX_CMDS) {
+	uint8_t cmd = req->msg.command;
+	uint8_t seq = req->msg.seq;
+
+	if (cmd > NUM_MBOX_CMDS) {
 		MSG_ERR("Unknown mbox command: %d\n", cmd);
 		return -MBOX_R_PARAM_ERROR;
 	}
 
+	if (seq == context->prev_seq && cmd != MBOX_C_GET_MBOX_INFO) {
+		MSG_ERR("Invalid sequence number: %d\n", seq);
+		return -MBOX_R_SEQ_ERROR;
+	}
+
 	if (context->state & STATE_SUSPENDED) {
 		if (cmd != MBOX_C_GET_MBOX_INFO && cmd != MBOX_C_ACK) {
 			MSG_ERR("Cannot use that cmd while suspended: %d\n",
@@ -714,7 +722,7 @@
 	int rc = 0, len;
 
 	MSG_OUT("Got data in with command %d\n", req->msg.command);
-	rc = check_cmd_valid(context, req->msg.command);
+	rc = check_req_valid(context, req);
 	if (rc < 0) {
 		resp.response = -rc;
 	} else {
@@ -727,6 +735,8 @@
 		}
 	}
 
+	context->prev_seq = req->msg.seq;
+
 	MSG_OUT("Writing response to MBOX regs: %d\n", resp.response);
 	len = write(context->fds[MBOX_FD].fd, &resp, sizeof(resp));
 	if (len < sizeof(resp)) {