test: Use a local variable for test state

At present we use a global test state for all driver-model tests. Make use
of a local struct like we do with the other tests.

To make this work, add functions to get and set this state. When a test
starts, the state is set (so it can be used in the test). When a test
finishes, the state is unset, so it cannot be used by mistake.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/test/dm/test-dm.c b/test/dm/test-dm.c
index 826b645..cdaf27b 100644
--- a/test/dm/test-dm.c
+++ b/test/dm/test-dm.c
@@ -20,8 +20,6 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-struct unit_test_state global_dm_test_state;
-
 static bool test_matches(const char *test_name, const char *find_name)
 {
 	if (!find_name)
@@ -44,7 +42,7 @@
 {
 	struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
 	const int n_ents = ll_entry_count(struct unit_test, dm_test);
-	struct unit_test_state *uts = &global_dm_test_state;
+	struct unit_test_state uts_s = { .fail_count = 0 }, *uts = &uts_s;
 	struct unit_test *test;
 	int found;
 
diff --git a/test/dm/test-driver.c b/test/dm/test-driver.c
index 63dc9d3..02cb974 100644
--- a/test/dm/test-driver.c
+++ b/test/dm/test-driver.c
@@ -18,7 +18,6 @@
 #include <test/ut.h>
 
 int dm_testdrv_op_count[DM_TEST_OP_COUNT];
-static struct unit_test_state *uts = &global_dm_test_state;
 
 static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
 {
@@ -37,6 +36,8 @@
 
 static int test_bind(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	/* Private data should not be allocated */
 	ut_assert(!dev_get_priv(dev));
 
@@ -46,6 +47,7 @@
 
 static int test_probe(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
 	struct dm_test_priv *priv = dev_get_priv(dev);
 
 	/* Private data should be allocated */
@@ -58,6 +60,8 @@
 
 static int test_remove(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	/* Private data should still be allocated */
 	ut_assert(dev_get_priv(dev));
 
@@ -67,6 +71,8 @@
 
 static int test_unbind(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	/* Private data should not be allocated */
 	ut_assert(!dev_get_priv(dev));
 
@@ -116,6 +122,8 @@
 
 static int test_manual_probe(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
 	if (!uts->force_fail_alloc)
 		dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
diff --git a/test/dm/test-uclass.c b/test/dm/test-uclass.c
index f4b540c..0677017 100644
--- a/test/dm/test-uclass.c
+++ b/test/dm/test-uclass.c
@@ -17,8 +17,6 @@
 #include <test/test.h>
 #include <test/ut.h>
 
-static struct unit_test_state *uts = &global_dm_test_state;
-
 int test_ping(struct udevice *dev, int pingval, int *pingret)
 {
 	const struct test_ops *ops = device_get_ops(dev);
@@ -31,6 +29,7 @@
 
 static int test_post_bind(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
 	struct dm_test_perdev_uc_pdata *uc_pdata;
 
 	dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++;
@@ -56,6 +55,7 @@
 static int test_pre_probe(struct udevice *dev)
 {
 	struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
+	struct unit_test_state *uts = test_get_state();
 
 	dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]++;
 	ut_assert(priv);
@@ -66,6 +66,7 @@
 
 static int test_post_probe(struct udevice *dev)
 {
+	struct unit_test_state *uts = test_get_state();
 	struct udevice *prev = list_entry(dev->uclass_node.prev,
 					    struct udevice, uclass_node);
 
@@ -100,6 +101,8 @@
 
 static int test_init(struct uclass *uc)
 {
+	struct unit_test_state *uts = test_get_state();
+
 	dm_testdrv_op_count[DM_TEST_OP_INIT]++;
 	ut_assert(uclass_get_priv(uc));
 
diff --git a/test/test-main.c b/test/test-main.c
index 4e17c9e..139fc1f 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -16,6 +16,19 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/* This is valid when a test is running, NULL otherwise */
+static struct unit_test_state *cur_test_state;
+
+struct unit_test_state *test_get_state(void)
+{
+	return cur_test_state;
+}
+
+void test_set_state(struct unit_test_state *uts)
+{
+	cur_test_state = uts;
+}
+
 /**
  * dm_test_pre_run() - Get ready to run a driver model test
  *
@@ -180,6 +193,9 @@
 		note = " (flat tree)";
 	printf("Test: %s: %s%s\n", test_name, fname, note);
 
+	/* Allow access to test state from drivers */
+	test_set_state(uts);
+
 	ret = test_pre_run(uts, test);
 	if (ret == -EAGAIN)
 		return -EAGAIN;
@@ -192,6 +208,8 @@
 	if (ret)
 		return ret;
 
+	test_set_state( NULL);
+
 	return 0;
 }