From 79aa6ebf9d0b982c859a0c1bec1d879ff3d9ad29 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Horia=20Geant=C4=83?= <horia.geanta@nxp.com>
Date: Wed, 27 Nov 2019 12:30:41 +0200
Subject: [PATCH] LF-63-1 crypto: caam - fix SM test init
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Commit 77debf316c44 ("LFV-26 crypto: caam - fix Secure Memory driver init")
addressed SM driver initialization and also update SM test.

However, the fix for SM test is insufficient.
There are cases when SM test runs before SM driver, causing a crash
due to uninitialized "priv" pointer being dereferenced.

The fix consists in the following:

1. Since SM test is a "bare" device driver (doesn't sit on any bus),
there is no deferred probing support.
Thus we have no choice (*) but to abort SM tests with a notification.

(*) We don't want to force SM driver running first by means of
init levels etc. Just KISS.

2. SM test driver forced to being built only as a module
Since SM test driver's only goal is to run SM tests, it doesn't make
any sense to be built-in.
Building the driver as a module allows for running the tests
several times if needed (multiple modprobe & rmmod cycles).

Note: from the perspective of wanting to test repetitively, it would
make sense to force module unloading by returning an error code
in the module_init function.
However, this might affect test scripts (due to error code and/or
message output by unsuccessful module loading), so we postpone
this change for now.

Fixes: d02fe599d7d5 ("MLKU-25-3 crypto: caam - add Secure Memory support")
Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
Acked-by: Leonard Crestez <leonard.crestez@nxp.com>
---
 drivers/crypto/caam/Kconfig   | 1 +
 drivers/crypto/caam/Makefile  | 2 +-
 drivers/crypto/caam/sm_test.c | 4 ++++
 3 files changed, 6 insertions(+), 1 deletion(-)

--- a/drivers/crypto/caam/Kconfig
+++ b/drivers/crypto/caam/Kconfig
@@ -191,6 +191,7 @@ config CRYPTO_DEV_FSL_CAAM_SM_SLOTSIZE
 config CRYPTO_DEV_FSL_CAAM_SM_TEST
 	tristate "CAAM Secure Memory - Keystore Test/Example (EXPERIMENTAL)"
 	depends on CRYPTO_DEV_FSL_CAAM_SM
+	depends on m
 	help
 	  Example thread to exercise the Keystore API and to verify that
 	  stored and recovered secrets can be used for general purpose
--- a/drivers/crypto/caam/Makefile
+++ b/drivers/crypto/caam/Makefile
@@ -24,7 +24,7 @@ caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHA
 caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API) += caamrng.o
 caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC_API) += caampkc.o pkc_desc.o
 caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_SM) += sm_store.o
-caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_SM_TEST) += sm_test.o
+obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_SM_TEST) += sm_test.o
 caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_SECVIO) += secvio.o
 #caam-jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_TK_API) += tag_object.o
 
--- a/drivers/crypto/caam/sm_test.c
+++ b/drivers/crypto/caam/sm_test.c
@@ -552,6 +552,10 @@ static int __init caam_sm_test_init(void
 	of_node_put(dev_node);
 
 	priv = dev_get_drvdata(&pdev->dev);
+	if (!priv) {
+		dev_info(&pdev->dev, "SM driver not ready, aborting tests\n");
+		return -ENODEV;
+	}
 	if (!priv->sm_present) {
 		dev_info(&pdev->dev, "No SM support, skipping tests\n");
 		return -ENODEV;
