Clean up better on error.

Free mailbox buffers and release the RX buffer in case it was in use.

Change-Id: I9f84a70a723b371380a9bedc2ca6f6effbd39478
diff --git a/main.c b/main.c
index b2041ef..67c740a 100644
--- a/main.c
+++ b/main.c
@@ -829,6 +829,16 @@
 	}
 
 	kfree(hf_vms);
+
+	ffa_rx_release();
+	if (hf_send_page) {
+		__free_page(hf_send_page);
+		hf_send_page = NULL;
+	}
+	if (hf_recv_page) {
+		__free_page(hf_recv_page);
+		hf_recv_page = NULL;
+	}
 }
 
 /**
@@ -971,6 +981,7 @@
 	hf_recv_page = alloc_page(GFP_KERNEL);
 	if (!hf_recv_page) {
 		__free_page(hf_send_page);
+		hf_send_page = NULL;
 		pr_err("Unable to allocate receive buffer\n");
 		return -ENOMEM;
 	}
@@ -983,14 +994,13 @@
 	ffa_ret = ffa_rxtx_map(page_to_phys(hf_send_page),
 				 page_to_phys(hf_recv_page));
 	if (ffa_ret.func != FFA_SUCCESS_32) {
-		__free_page(hf_send_page);
-		__free_page(hf_recv_page);
-		pr_err("Unable to configure VM\n");
+		pr_err("Unable to configure VM mailbox.\n");
 		if (ffa_ret.func == FFA_ERROR_32)
 			pr_err("FF-A error code %d\n", ffa_ret.arg2);
 		else
 			pr_err("Unexpected FF-A function %#x\n", ffa_ret.func);
-		return -EIO;
+		ret = -EIO;
+		goto fail_with_cleanup;
 	}
 
 	/* Get the number of secondary VMs. */
@@ -1004,14 +1014,17 @@
 	if (secondary_vm_count > CONFIG_HAFNIUM_MAX_VMS - 1) {
 		pr_err("Number of VMs is out of range: %d\n",
 		       secondary_vm_count);
-		return -EDQUOT;
+		ret = -EDQUOT;
+		goto fail_with_cleanup;
 	}
 
 	/* Only track the secondary VMs. */
 	hf_vms = kmalloc_array(secondary_vm_count, sizeof(struct hf_vm),
 			       GFP_KERNEL);
-	if (!hf_vms)
-		return -ENOMEM;
+	if (!hf_vms) {
+		ret = -ENOMEM;
+		goto fail_with_cleanup;
+	}
 
 	/* Cache the VM id for later usage. */
 	current_vm_id = hf_vm_get_id();