Skip to contentSkip to conversation

scsi: ufs: add inline crypto support to UFS HCD

PATCH In review Parshuram Thombare 6 participants 11 messages Dec 11, 2018 – Dec 14, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 2/2 scsi: ufs: add inline crypto support to UFS HCD

Add real time crypto support to UFS HCD using new device mapper 'crypto-ufs'. dmsetup tool can be used to enable real time / inline crypto support using device mapper 'crypt-ufs'.

---
 MAINTAINERS                      |    7 +
 block/Kconfig                    |    5 +
 drivers/scsi/ufs/Kconfig         |   12 +
 drivers/scsi/ufs/Makefile        |    1 +
 drivers/scsi/ufs/ufshcd-crypto.c |  453 ++++++++++++++++++++++++++++++++++++++
 drivers/scsi/ufs/ufshcd-crypto.h |  102 +++++++++
 drivers/scsi/ufs/ufshcd.c        |   27 +++-
 drivers/scsi/ufs/ufshcd.h        |    6 +
 drivers/scsi/ufs/ufshci.h        |    1 +
 9 files changed, 613 insertions(+), 1 deletions(-)
 create mode 100644 drivers/scsi/ufs/ufshcd-crypto.c
 create mode 100644 drivers/scsi/ufs/ufshcd-crypto.h
MAINTAINERS +7 -0
index f485597..3a68126 100644
@@ -15340,6 +15340,13 @@ S:	Supported
 F:	Documentation/scsi/ufs.txt
 F:	drivers/scsi/ufs/
 
+UNIVERSAL FLASH STORAGE HOST CONTROLLER CRYPTO DRIVER
+M:	Parshuram Thombare <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/scsi/ufs/ufshcd-crypto.c
+F:	drivers/scsi/ufs/ufshcd-crypto.h
+
 UNIVERSAL FLASH STORAGE HOST CONTROLLER DRIVER DWC HOOKS
 M:	Joao Pinto <[email protected]>
 L:	[email protected]
block/Kconfig +5 -0
index f7045aa..6afe131 100644
@@ -224,4 +224,9 @@ config BLK_MQ_RDMA
 config BLK_PM
 	def_bool BLOCK && PM
 
+config BLK_DEV_HW_RT_ENCRYPTION
+	bool
+	depends on SCSI_UFSHCD_RT_ENCRYPTION
+	default n
+
 source block/Kconfig.iosched
drivers/scsi/ufs/Kconfig +12 -0
index 2ddbb26..09a3ec0 100644
@@ -136,3 +136,15 @@ config SCSI_UFS_BSG
 
 	  Select this if you need a bsg device node for your UFS controller.
 	  If unsure, say N.
+
+config SCSI_UFSHCD_RT_ENCRYPTION
+	bool "Universal Flash Storage Controller RT encryption support"
+	depends on SCSI_UFSHCD
+	default n
+	select BLK_DEV_HW_RT_ENCRYPTION if SCSI_UFSHCD_RT_ENCRYPTION
+	select BLK_DEV_DM if SCSI_UFSHCD_RT_ENCRYPTION
+	help
+	Universal Flash Storage Controller RT encryption support
+
+	Select this if you want to enable real time encryption on UFS controller
+	If unsure, say N.
drivers/scsi/ufs/Makefile +1 -0
index a3bd70c..6169096 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_SCSI_UFS_QCOM) += ufs-qcom.o
 obj-$(CONFIG_SCSI_UFSHCD) += ufshcd-core.o
 ufshcd-core-y				+= ufshcd.o ufs-sysfs.o
 ufshcd-core-$(CONFIG_SCSI_UFS_BSG)	+= ufs_bsg.o
+ufshcd-core-$(CONFIG_SCSI_UFSHCD_RT_ENCRYPTION) += ufshcd-crypto.o
 obj-$(CONFIG_SCSI_UFSHCD_PCI) += ufshcd-pci.o
 obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o
 obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o
drivers/scsi/ufs/ufshcd-crypto.c new file +453 -0

453 lines not on this page · raw message

drivers/scsi/ufs/ufshcd-crypto.h new file +102 -0

102 lines not on this page · raw message

drivers/scsi/ufs/ufshcd.c +26 -1
index 86fe114..a96b038 100644
@@ -47,6 +47,9 @@
 #include "unipro.h"
 #include "ufs-sysfs.h"
 #include "ufs_bsg.h"
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+#include "ufshcd-crypto.h"
+#endif
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/ufs.h>
@@ -2198,6 +2201,14 @@ static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
 
 	dword_0 = data_direction | (lrbp->command_type
 				<< UPIU_COMMAND_TYPE_OFFSET);
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	if (lrbp->cci >= 0) {
+		dword_0 |= (1 << CRYPTO_UTP_REQ_DESC_DWORD0_CE_SHIFT);
+		dword_0 |= ((lrbp->cci << CRYPTO_UTP_REQ_DESC_DWORD0_CCI_SHIFT)
+				& CRYPTO_UTP_REQ_DESC_DWORD0_CCI_MASK);
+	} else
+		dword_0 &= ~CRYPTO_UTP_REQ_DESC_DWORD0_CE_MASK;
+#endif
 	if (lrbp->intr_cmd)
 		dword_0 |= UTP_REQ_DESC_INT_CMD;
 
@@ -2462,6 +2473,12 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
 	lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
 	lrbp->req_abort_skip = false;
 
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	lrbp->cci = -1;
+	/* prepare block for crypto */
+	if (hba->capabilities & MASK_CRYPTO_SUPPORT)
+		ufshcd_prepare_for_crypto(hba, lrbp);
+#endif
 	ufshcd_comp_scsi_upiu(hba, lrbp);
 
 	err = ufshcd_map_sg(hba, lrbp);
@@ -8119,6 +8136,11 @@ void ufshcd_remove(struct ufs_hba *hba)
 	ufs_bsg_remove(hba);
 	ufs_sysfs_remove_nodes(hba->dev);
 	scsi_remove_host(hba->host);
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	if (hba->capabilities & MASK_CRYPTO_SUPPORT)
+		ufshcd_crypto_remove(hba);
+#endif
+
 	/* disable interrupts */
 	ufshcd_disable_intr(hba, hba->intr_mask);
 	ufshcd_hba_stop(hba, true);
@@ -8346,7 +8368,10 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 		hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
 			    FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
 	}
-
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	if (hba->capabilities & MASK_CRYPTO_SUPPORT)
+		ufshcd_crypto_init(hba);
+#endif
 	/* Hold auto suspend until async scan completes */
 	pm_runtime_get_sync(dev);
 	atomic_set(&hba->scsi_block_reqs_cnt, 0);
drivers/scsi/ufs/ufshcd.h +6 -0
index 69ba744..25755c8 100644
@@ -193,6 +193,9 @@ struct ufshcd_lrb {
 	ktime_t compl_time_stamp;
 
 	bool req_abort_skip;
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	int cci;
+#endif
 };
 
 /**
@@ -706,6 +709,9 @@ struct ufs_hba {
 
 	struct device		bsg_dev;
 	struct request_queue	*bsg_queue;
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	struct ufshcd_crypto_ctx *cctx;
+#endif
 };
 
 /* Returns true if clocks can be gated. Otherwise false */
drivers/scsi/ufs/ufshci.h +1 -0
index 6fa889d..fe5a92d 100644
@@ -90,6 +90,7 @@ enum {
 	MASK_64_ADDRESSING_SUPPORT		= 0x01000000,
 	MASK_OUT_OF_ORDER_DATA_DELIVERY_SUPPORT	= 0x02000000,
 	MASK_UIC_DME_TEST_MODE_SUPPORT		= 0x04000000,
+	MASK_CRYPTO_SUPPORT                     = 0x10000000,
 };
 
 #define UFS_MASK(mask, offset)		((mask) << (offset))
Signature · 1 line
1.7.1
#1 lore raw
Quoted 6 lines · Parshuram Thombare

Add real time crypto support to UFS HCD using new device mapper 'crypto-ufs'. dmsetup tool can be used to enable real time / inline crypto support using device mapper 'crypt-ufs'.

As you cc:ed me, I'll provide some minor review comments:

block/Kconfig config BLK_MQ_RDMA · line 230

Quoted 4 lines
block/Kconfig +4 -0
+config BLK_DEV_HW_RT_ENCRYPTION
+	bool
+	depends on SCSI_UFSHCD_RT_ENCRYPTION
+	default n

n is always the default, you never need to list that.

Quoted 7 lines
block/Kconfig +1 -0
+
 source block/Kconfig.iosched
drivers/scsi/ufs/Kconfig +0 -0
index 2ddbb26..09a3ec0 100644

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 143

Quoted 8 lines
drivers/scsi/ufs/Kconfig +5 -0
@@ -136,3 +136,15 @@ config SCSI_UFS_BSG
 
 	  Select this if you need a bsg device node for your UFS controller.
 	  If unsure, say N.
+
+config SCSI_UFSHCD_RT_ENCRYPTION
+	bool "Universal Flash Storage Controller RT encryption support"
+	depends on SCSI_UFSHCD
+	default n

Same here.

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 150

Quoted 7 lines
drivers/scsi/ufs/Kconfig +7 -0
+	select BLK_DEV_HW_RT_ENCRYPTION if SCSI_UFSHCD_RT_ENCRYPTION
+	select BLK_DEV_DM if SCSI_UFSHCD_RT_ENCRYPTION
+	help
+	Universal Flash Storage Controller RT encryption support
+
+	Select this if you want to enable real time encryption on UFS controller
+	If unsure, say N.

Don't you need to indent the help lines?

drivers/scsi/ufs/Kconfig

Quoted 4 lines
drivers/scsi/ufs/Kconfig +4 -0
+int ufshcd_crypto_init(struct ufs_hba *hba);
+void ufshcd_crypto_remove(struct ufs_hba *hba);
+void ufshcd_prepare_for_crypto(struct ufs_hba *hba, struct ufshcd_lrb *lrbp);
+#endif

You need to provide inline functions for when your config option is not enabled here.

That way you don't have to do this mess:

drivers/scsi/ufs/ufshcd.c line 52

Quoted 11 lines
drivers/scsi/ufs/ufshcd.c +3 -0
index 86fe114..a96b038 100644
@@ -47,6 +47,9 @@
 #include "unipro.h"
 #include "ufs-sysfs.h"
 #include "ufs_bsg.h"
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+#include "ufshcd-crypto.h"
+#endif

No need for #ifdefs in .c files at all. Always include the .h file, and then since your functions are all inline void functions, the code just compiles away into nothing.

Quoted 5 lines
drivers/scsi/ufs/ufshcd.c +0 -0
 #define CREATE_TRACE_POINTS
 #include <trace/events/ufs.h>
@@ -2198,6 +2201,14 @@ static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
 

drivers/scsi/ufs/ufshcd.c static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp, · line 2211

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +8 -0
 	dword_0 = data_direction | (lrbp->command_type
 				<< UPIU_COMMAND_TYPE_OFFSET);
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	if (lrbp->cci >= 0) {
+		dword_0 |= (1 << CRYPTO_UTP_REQ_DESC_DWORD0_CE_SHIFT);
+		dword_0 |= ((lrbp->cci << CRYPTO_UTP_REQ_DESC_DWORD0_CCI_SHIFT)
+				& CRYPTO_UTP_REQ_DESC_DWORD0_CCI_MASK);
+	} else
+		dword_0 &= ~CRYPTO_UTP_REQ_DESC_DWORD0_CE_MASK;
+#endif

Some comments on what this is trying to do here?

drivers/scsi/ufs/ufshcd.c static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) · line 2477

Quoted 9 lines
drivers/scsi/ufs/ufshcd.c +2 -0
 	if (lrbp->intr_cmd)
 		dword_0 |= UTP_REQ_DESC_INT_CMD;
 
@@ -2462,6 +2473,12 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
 	lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
 	lrbp->req_abort_skip = false;
 
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	lrbp->cci = -1;

What does -1 mean? You should have a type for it if it is something "special".

drivers/scsi/ufs/ufshcd.c static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) · line 2481

Quoted 4 lines
drivers/scsi/ufs/ufshcd.c +4 -0
+	/* prepare block for crypto */
+	if (hba->capabilities & MASK_CRYPTO_SUPPORT)
+		ufshcd_prepare_for_crypto(hba, lrbp);
+#endif

Again, no #ifdefs needed please.

drivers/scsi/ufs/ufshcd.c void ufshcd_remove(struct ufs_hba *hba) · line 8142

Quoted 11 lines
drivers/scsi/ufs/ufshcd.c +4 -0
 	ufshcd_comp_scsi_upiu(hba, lrbp);
 
 	err = ufshcd_map_sg(hba, lrbp);
@@ -8119,6 +8136,11 @@ void ufshcd_remove(struct ufs_hba *hba)
 	ufs_bsg_remove(hba);
 	ufs_sysfs_remove_nodes(hba->dev);
 	scsi_remove_host(hba->host);
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	if (hba->capabilities & MASK_CRYPTO_SUPPORT)
+		ufshcd_crypto_remove(hba);
+#endif

Same ifdef here, and everywhere else in this file.

drivers/scsi/ufs/ufshcd.h struct ufshcd_lrb { · line 198

Quoted 9 lines
drivers/scsi/ufs/ufshcd.h +3 -0
@@ -193,6 +193,9 @@ struct ufshcd_lrb {
 	ktime_t compl_time_stamp;
 
 	bool req_abort_skip;
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	int cci;
+#endif

No need for a #ifdef here. But comment on exactly what "cci" means, that seems to not make any sense to me.

drivers/scsi/ufs/ufshcd.h struct ufs_hba { · line 714

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +3 -0
 };
 
 /**
@@ -706,6 +709,9 @@ struct ufs_hba {
 
 	struct device		bsg_dev;
 	struct request_queue	*bsg_queue;
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	struct ufshcd_crypto_ctx *cctx;
+#endif

or here.

drivers/scsi/ufs/ufshci.h enum { · line 93

Quoted 12 lines
drivers/scsi/ufs/ufshcd.h +0 -0
 };
 
 /* Returns true if clocks can be gated. Otherwise false */
drivers/scsi/ufs/ufshci.h +1 -0
index 6fa889d..fe5a92d 100644
@@ -90,6 +90,7 @@ enum {
 	MASK_64_ADDRESSING_SUPPORT		= 0x01000000,
 	MASK_OUT_OF_ORDER_DATA_DELIVERY_SUPPORT	= 0x02000000,
 	MASK_UIC_DME_TEST_MODE_SUPPORT		= 0x04000000,
+	MASK_CRYPTO_SUPPORT                     = 0x10000000,

Why are you all not using the BIT(N) macro for these?

thanks,

greg k-h

#2 lore raw

Hello Greg,

Thank you for comments.

Quoted 23 lines

-----Original Message-----
From: Greg KH <[email protected]>
Sent: Tuesday, December 11, 2018 5:34 PM
To: Parshuram Raju Thombare <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Alan Douglas <[email protected]>; Janek Kotas <[email protected]>; Rafal Ciepiela <[email protected]>
Subject: Re: [PATCH 2/2] scsi: ufs: add inline crypto support to UFS HCD

EXTERNAL MAIL

Quoted 5 lines · Parshuram Thombare

Add real time crypto support to UFS HCD using new device mapper 'crypto-ufs'. dmsetup tool can be used to enable real time / inline crypto support using device mapper 'crypt-ufs'.

Quoted 8 lines

As you cc:ed me, I'll provide some minor review comments:

block/Kconfig config BLK_MQ_RDMA · line 230

Quoted 4 lines
block/Kconfig +4 -0
+config BLK_DEV_HW_RT_ENCRYPTION
+	bool
+	depends on SCSI_UFSHCD_RT_ENCRYPTION
+	default n

n is always the default, you never need to list that.

Ok, I will remove it.

Quoted 9 lines
Quoted 9 lines
block/Kconfig +1 -0
+
 source block/Kconfig.iosched
drivers/scsi/ufs/Kconfig index renamed from drivers/scsi/ufs/Kconfig +0 -0

2ddbb26..09a3ec0 100644

drivers/scsi/ufs/Kconfig +0 -0
@@ -136,3 +136,15 @@ config SCSI_UFS_BSG
 
Quoted 10 lines

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 143

Quoted 7 lines
drivers/scsi/ufs/Kconfig +5 -0
 	  Select this if you need a bsg device node for your UFS controller.
 	  If unsure, say N.
+
+config SCSI_UFSHCD_RT_ENCRYPTION
+	bool "Universal Flash Storage Controller RT encryption support"
+	depends on SCSI_UFSHCD
+	default n

Same here.

Ok, I will remove it.

Quoted 10 lines

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 150

Quoted 7 lines
drivers/scsi/ufs/Kconfig +7 -0
+	select BLK_DEV_HW_RT_ENCRYPTION if SCSI_UFSHCD_RT_ENCRYPTION
+	select BLK_DEV_DM if SCSI_UFSHCD_RT_ENCRYPTION
+	help
+	Universal Flash Storage Controller RT encryption support
+
+	Select this if you want to enable real time encryption on UFS controller
+	If unsure, say N.

Don't you need to indent the help lines?

Sorry, missed indentation check here. I will indent this.

Quoted 10 lines

drivers/scsi/ufs/Kconfig

Quoted 4 lines
drivers/scsi/ufs/Kconfig +4 -0
+int ufshcd_crypto_init(struct ufs_hba *hba); void
+ufshcd_crypto_remove(struct ufs_hba *hba); void
+ufshcd_prepare_for_crypto(struct ufs_hba *hba, struct ufshcd_lrb
+*lrbp); #endif

You need to provide inline functions for when your config option is not enabled here.

That way you don't have to do this mess:

Do you mean empty inline function (which are exported) in #else to avoid #ifdef around each call for these functions ?

Quoted 5 lines
Quoted 5 lines
drivers/scsi/ufs/ufshcd.c +0 -0
index 86fe114..a96b038 100644
@@ -47,6 +47,9 @@
Quoted 10 lines

drivers/scsi/ufs/ufshcd.c line 52

Quoted 5 lines
drivers/scsi/ufs/ufshcd.c +2 -0
 #include "unipro.h"
 #include "ufs-sysfs.h"
 #include "ufs_bsg.h"
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION #include "ufshcd-crypto.h"
+#endif

No need for #ifdefs in .c files at all. Always include the .h file, and then since your functions are all inline void functions, the code just compiles away into nothing.

Ok, I will remove it.

Quoted 14 lines

drivers/scsi/ufs/ufshcd.c static void ufshcd_prepare_req_desc_hdr(struct · line 2207

Quoted 11 lines
drivers/scsi/ufs/ufshcd.c +3 -0
 #define CREATE_TRACE_POINTS
 #include <trace/events/ufs.h>
@@ -2198,6 +2201,14 @@ static void ufshcd_prepare_req_desc_hdr(struct
 ufshcd_lrb *lrbp,
 
 	dword_0 = data_direction | (lrbp->command_type
 				<< UPIU_COMMAND_TYPE_OFFSET);
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	if (lrbp->cci >= 0) {
+		dword_0 |= (1 <<

CRYPTO_UTP_REQ_DESC_DWORD0_CE_SHIFT);

drivers/scsi/ufs/ufshcd.c static void ufshcd_prepare_req_desc_hdr(struct · line 2208

Quoted 1 line
drivers/scsi/ufs/ufshcd.c +1 -0
+		dword_0 |= ((lrbp->cci <<

CRYPTO_UTP_REQ_DESC_DWORD0_CCI_SHIFT)

Quoted 9 lines

drivers/scsi/ufs/ufshcd.c static void ufshcd_prepare_req_desc_hdr(struct · line 2209

Quoted 1 line
drivers/scsi/ufs/ufshcd.c +1 -0
+				&

CRYPTO_UTP_REQ_DESC_DWORD0_CCI_MASK);

drivers/scsi/ufs/ufshcd.c static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp, · line 2211

Quoted 3 lines
drivers/scsi/ufs/ufshcd.c +3 -0
+	} else
+		dword_0 &= ~CRYPTO_UTP_REQ_DESC_DWORD0_CE_MASK;
+#endif

Some comments on what this is trying to do here?

This is to enable encryption by setting CCI (crypto config index) and CE crypto enable bit. I will add comment here.

Quoted 5 lines

drivers/scsi/ufs/ufshcd.c static int ufshcd_queuecommand(struct Scsi_Host

Quoted 4 lines
drivers/scsi/ufs/ufshcd.c +0 -0
 	if (lrbp->intr_cmd)
 		dword_0 |= UTP_REQ_DESC_INT_CMD;
@@ -2462,6 +2473,12 @@ static int ufshcd_queuecommand(struct Scsi_Host

*host, struct scsi_cmnd *cmd)

Quoted 8 lines

drivers/scsi/ufs/ufshcd.c static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) · line 2477

Quoted 5 lines
drivers/scsi/ufs/ufshcd.c +2 -0
 	lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
 	lrbp->req_abort_skip = false;
 
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	lrbp->cci = -1;

What does -1 mean? You should have a type for it if it is something "special".

-1 means crypto is not enabled or supported by hardware. I think this need a comment. I will add it.

Quoted 7 lines

drivers/scsi/ufs/ufshcd.c static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)

Quoted 3 lines
drivers/scsi/ufs/ufshcd.c +3 -0
+	/* prepare block for crypto */
+	if (hba->capabilities & MASK_CRYPTO_SUPPORT)
+		ufshcd_prepare_for_crypto(hba, lrbp); #endif

Again, no #ifdefs needed please.

Ok, I will remove it.

Quoted 14 lines

drivers/scsi/ufs/ufshcd.c void ufshcd_remove(struct ufs_hba *hba) · line 8142

Quoted 11 lines
drivers/scsi/ufs/ufshcd.c +4 -0
 	ufshcd_comp_scsi_upiu(hba, lrbp);
 
 	err = ufshcd_map_sg(hba, lrbp);
@@ -8119,6 +8136,11 @@ void ufshcd_remove(struct ufs_hba *hba)
 	ufs_bsg_remove(hba);
 	ufs_sysfs_remove_nodes(hba->dev);
 	scsi_remove_host(hba->host);
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	if (hba->capabilities & MASK_CRYPTO_SUPPORT)
+		ufshcd_crypto_remove(hba);
+#endif

Same ifdef here, and everywhere else in this file.

Ok, I will remove it.

Quoted 5 lines
Quoted 5 lines
drivers/scsi/ufs/ufshcd.h +0 -0
@@ -193,6 +193,9 @@ struct ufshcd_lrb {
 	ktime_t compl_time_stamp;
 
Quoted 8 lines

drivers/scsi/ufs/ufshcd.h struct ufshcd_lrb { · line 198

Quoted 4 lines
drivers/scsi/ufs/ufshcd.h +3 -0
 	bool req_abort_skip;
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	int cci;
+#endif

No need for a #ifdef here. But comment on exactly what "cci" means, that seems to not make any sense to me.

CCI is crypto config index / slot in UFS. I will add a comment here.

Quoted 5 lines
Quoted 5 lines
drivers/scsi/ufs/ufshcd.h +0 -0
 };
 
 /**
@@ -706,6 +709,9 @@ struct ufs_hba {
 
Quoted 8 lines

drivers/scsi/ufs/ufshcd.h struct ufs_hba { · line 714

Quoted 5 lines
drivers/scsi/ufs/ufshcd.h +3 -0
 	struct device		bsg_dev;
 	struct request_queue	*bsg_queue;
+#ifdef CONFIG_SCSI_UFSHCD_RT_ENCRYPTION
+	struct ufshcd_crypto_ctx *cctx;
+#endif

or here.

Ok, I will remove it.

Quoted 14 lines

drivers/scsi/ufs/ufshci.h enum { · line 93

Quoted 12 lines
drivers/scsi/ufs/ufshcd.h +0 -0
 };
 
 /* Returns true if clocks can be gated. Otherwise false */ diff --git

a/drivers/scsi/ufs/ufshci.h b/drivers/scsi/ufs/ufshci.h index 6fa889d..fe5a92d 100644

drivers/scsi/ufs/ufshci.h +1 -0
@@ -90,6 +90,7 @@ enum {
 	MASK_64_ADDRESSING_SUPPORT		= 0x01000000,
 	MASK_OUT_OF_ORDER_DATA_DELIVERY_SUPPORT	= 0x02000000,
 	MASK_UIC_DME_TEST_MODE_SUPPORT		= 0x04000000,
+	MASK_CRYPTO_SUPPORT                     = 0x10000000,

Why are you all not using the BIT(N) macro for these?

Yes, I will replace it with BIT(N).

Quoted 4 lines

thanks,

greg k-h

Regards,
Parshuram Thombare

#3 lore raw
In reply to Parshuram Thombare
Quoted 4 lines · Parshuram Thombare

Add real time crypto support to UFS HCD using new device mapper 'crypto-ufs'. dmsetup tool can be used to enable real time / inline crypto support using device mapper 'crypt-ufs'.

Can you point to where in the UFS spec this support is specified?

#4 lore raw
In reply to Parshuram Thombare

[+Cc other people who have been working on this]

Hi Parshuram,

Quoted 242 lines · Parshuram Thombare

Add real time crypto support to UFS HCD using new device mapper 'crypto-ufs'. dmsetup tool can be used to enable real time / inline crypto support using device mapper 'crypt-ufs'.

---
 MAINTAINERS                      |    7 +
 block/Kconfig                    |    5 +
 drivers/scsi/ufs/Kconfig         |   12 +
 drivers/scsi/ufs/Makefile        |    1 +
 drivers/scsi/ufs/ufshcd-crypto.c |  453 ++++++++++++++++++++++++++++++++++++++
 drivers/scsi/ufs/ufshcd-crypto.h |  102 +++++++++
 drivers/scsi/ufs/ufshcd.c        |   27 +++-
 drivers/scsi/ufs/ufshcd.h        |    6 +
 drivers/scsi/ufs/ufshci.h        |    1 +
 9 files changed, 613 insertions(+), 1 deletions(-)
 create mode 100644 drivers/scsi/ufs/ufshcd-crypto.c
 create mode 100644 drivers/scsi/ufs/ufshcd-crypto.h
MAINTAINERS +7 -0
index f485597..3a68126 100644
@@ -15340,6 +15340,13 @@ S:	Supported
 F:	Documentation/scsi/ufs.txt
 F:	drivers/scsi/ufs/
 
+UNIVERSAL FLASH STORAGE HOST CONTROLLER CRYPTO DRIVER
+M:	Parshuram Thombare <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/scsi/ufs/ufshcd-crypto.c
+F:	drivers/scsi/ufs/ufshcd-crypto.h
+
 UNIVERSAL FLASH STORAGE HOST CONTROLLER DRIVER DWC HOOKS
 M:	Joao Pinto <[email protected]>
 L:	[email protected]
block/Kconfig +5 -0
index f7045aa..6afe131 100644
@@ -224,4 +224,9 @@ config BLK_MQ_RDMA
 config BLK_PM
 	def_bool BLOCK && PM
 
+config BLK_DEV_HW_RT_ENCRYPTION
+	bool
+	depends on SCSI_UFSHCD_RT_ENCRYPTION
+	default n
+
 source block/Kconfig.iosched
drivers/scsi/ufs/Kconfig +12 -0
index 2ddbb26..09a3ec0 100644
@@ -136,3 +136,15 @@ config SCSI_UFS_BSG
 
 	  Select this if you need a bsg device node for your UFS controller.
 	  If unsure, say N.
+
+config SCSI_UFSHCD_RT_ENCRYPTION
+	bool "Universal Flash Storage Controller RT encryption support"
+	depends on SCSI_UFSHCD
+	default n
+	select BLK_DEV_HW_RT_ENCRYPTION if SCSI_UFSHCD_RT_ENCRYPTION
+	select BLK_DEV_DM if SCSI_UFSHCD_RT_ENCRYPTION
+	help
+	Universal Flash Storage Controller RT encryption support
+
+	Select this if you want to enable real time encryption on UFS controller
+	If unsure, say N.
drivers/scsi/ufs/Makefile +1 -0
index a3bd70c..6169096 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_SCSI_UFS_QCOM) += ufs-qcom.o
 obj-$(CONFIG_SCSI_UFSHCD) += ufshcd-core.o
 ufshcd-core-y				+= ufshcd.o ufs-sysfs.o
 ufshcd-core-$(CONFIG_SCSI_UFS_BSG)	+= ufs_bsg.o
+ufshcd-core-$(CONFIG_SCSI_UFSHCD_RT_ENCRYPTION) += ufshcd-crypto.o
 obj-$(CONFIG_SCSI_UFSHCD_PCI) += ufshcd-pci.o
 obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o
 obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o
drivers/scsi/ufs/ufshcd-crypto.c new file +152 -0
new file mode 100644
index 0000000..9c95ff3
@@ -0,0 +1,453 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * UFS Host controller crypto driver
+ *
+ * Copyright (C) 2018 Cadence Design Systems, Inc.
+ *
+ * Authors:
+ *	Parshuram Thombare <[email protected]>
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <crypto/aes.h>
+#include <linux/device-mapper.h>
+#include "ufshcd.h"
+#include "ufshcd-crypto.h"
+#include "scsi/scsi_device.h"
+#include "scsi/scsi_host.h"
+
+struct ufshcd_dm_ctx {
+	struct dm_dev *dev;
+	sector_t start;
+	unsigned short int sector_size;
+	unsigned char sector_shift;
+	int cci;
+	int cap_idx;
+	char key[AES_MAX_KEY_SIZE];
+	struct ufs_hba *hba;
+};
+
+static int dm_registered;
+
+static inline int
+ufshcd_key_id_to_len(int key_id)
+{
+	int key_len = -1;
+
+	switch (key_id) {
+	case UFS_CRYPTO_KEY_ID_128BITS:
+		key_len = AES_KEYSIZE_128;
+		break;
+	case UFS_CRYPTO_KEY_ID_192BITS:
+		key_len = AES_KEYSIZE_192;
+		break;
+	case UFS_CRYPTO_KEY_ID_256BITS:
+		key_len = AES_KEYSIZE_256;
+		break;
+	default:
+		break;
+	}
+	return key_len;
+}
+
+static inline int
+ufshcd_key_len_to_id(int key_len)
+{
+	int key_id = -1;
+
+	switch (key_len) {
+	case AES_KEYSIZE_128:
+		key_id = UFS_CRYPTO_KEY_ID_128BITS;
+		break;
+	case AES_KEYSIZE_192:
+		key_id = UFS_CRYPTO_KEY_ID_192BITS;
+		break;
+	case AES_KEYSIZE_256:
+		key_id = UFS_CRYPTO_KEY_ID_256BITS;
+		break;
+	default:
+		break;
+	}
+	return key_id;
+}
+
+static void
+ufshcd_read_crypto_capabilities(struct ufs_hba *hba)
+{
+	u32 tmp, i;
+	struct ufshcd_crypto_ctx *cctx = hba->cctx;
+
+	for (i = 0; i < cctx->cap_cnt; i++) {
+		tmp = ufshcd_readl(hba, REG_UFS_CRYPTOCAP + i);
+		cctx->ccaps[i].key_id = (tmp & CRYPTO_CAPS_KS_MASK) >>
+						CRYPTO_CAPS_KS_SHIFT;
+		cctx->ccaps[i].sdusb = (tmp & CRYPTO_CAPS_SDUSB_MASK) >>
+						CRYPTO_CAPS_SDUSB_SHIFT;
+		cctx->ccaps[i].alg_id = (tmp & CRYPTO_CAPS_ALG_ID_MASK) >>
+						CRYPTO_CAPS_ALG_ID_SHIFT;
+	}
+}
+
+static inline int
+ufshcd_get_cap_idx(struct ufshcd_crypto_ctx *cctx, int alg_id,
+	int key_id)
+{
+	int cap_idx;
+
+	for (cap_idx = 0; cap_idx < cctx->cap_cnt; cap_idx++) {
+		if (((cctx->ccaps[cap_idx].alg_id == alg_id) &&
+			cctx->ccaps[cap_idx].key_id == key_id))
+			break;
+	}
+	return ((cap_idx < cctx->cap_cnt) ? cap_idx : -1);
+}
+
+static inline int
+ufshcd_get_cci_slot(struct ufshcd_crypto_ctx *cctx)
+{
+	int  cci;
+
+	for (cci = 0; cci < cctx->config_cnt; cci++) {
+		if (!cctx->cconfigs[cci].cfge) {
+			cctx->cconfigs[cci].cfge = 1;
+			break;
+		}
+	}
+	return ((cci < cctx->config_cnt) ? cci : -1);
+}
+
+static void
+ufshcd_aes_ecb_set_key(struct ufshcd_dm_ctx *ctx)
+{
+	int i, key_size;
+	u32 val, cconfig16, crypto_config_addr;
+	struct ufshcd_crypto_ctx *cctx;
+	struct ufshcd_crypto_config *cconfig;
+	struct ufshcd_crypto_cap ccap;
+
+	cctx = ctx->hba->cctx;
+	if (ctx->cci <= 0)
+		ctx->cci = ufshcd_get_cci_slot(cctx);
+	/* If no slot is available, slot 0 is shared */
+	ctx->cci = ctx->cci < 0 ? 0 : ctx->cci;
+	cconfig = &(cctx->cconfigs[ctx->cci]);
+	ccap = cctx->ccaps[ctx->cap_idx];
+	key_size = ufshcd_key_id_to_len(ccap.key_id);
+
+	if ((cconfig->cap_idx != ctx->cap_idx) ||
+		((key_size > 0) &&
+		memcmp(cconfig->key, ctx->key, key_size))) {
+		cconfig->cap_idx = ctx->cap_idx;
+		memcpy(cconfig->key, ctx->key, key_size);
+		crypto_config_addr = cctx->crypto_config_base_addr +
+				ctx->cci * CRYPTO_CONFIG_SIZE;
+		cconfig16 = ccap.sdusb | (1 << CRYPTO_CCONFIG16_CFGE_SHIFT);
+		cconfig16 |= ((ctx->cap_idx << CRYPTO_CCONFIG16_CAP_IDX_SHIFT) &
+				CRYPTO_CCONFIG16_CAP_IDX_MASK);
+		spin_lock(&cctx->crypto_lock);
+		for (i = 0; i < key_size; i += 4) {
+			val = (ctx->key[i] | (ctx->key[i + 1] << 8) |
+				(ctx->key[i + 2] << 16) |

drivers/scsi/ufs/ufshcd-crypto.c line 162

Quoted 10 lines
drivers/scsi/ufs/ufshcd-crypto.c new file +10 -0
+				(ctx->key[i + 3] << 24));
+			ufshcd_writel(ctx->hba, val, crypto_config_addr + i);
+		}
+		ufshcd_writel(ctx->hba, cpu_to_le32(cconfig16),
+				crypto_config_addr + (4 * 16));
+		spin_unlock(&cctx->crypto_lock);
+		/* Make sure keys are programmed */
+		mb();
+	}
+}

First of all, thanks for working on this. A lot of Android device vendors would like to have upstream support for inline encryption. However, are you aware of previous (unsuccessful) patchsets by other people working on this? Have you addressed the concerns and improved on their work, or is this just yet another new effort starting from scratch?

AnilKumar Chimata <[email protected]> (Qualcomm) in October 2018:

	https://patchwork.kernel.org/cover/10645739/

Ladvine D Almeida <[email protected]> in May 2018:

http://lists-archives.com/linux-kernel/29135393-scsi-ufs-ufs-host-controller-crypto-changes.html

Satya Tangirala <[email protected]> is also working on it but I don't believe he's sent out a patchset yet.

It would be nice to coordinate to get a proper solution upstream that works for everyone, rather than having everyone try independently and fail repeatedly :-)

Also, note that ECB mode is not appropriate for disk encryption. So this patch (and the hardware you tested it on, if that's all it supports) is effectively useless as-is. You need to support XTS mode.

Thanks!

- Eric

#5 lore raw

Hello Eric,

Thank you for a comment.

Quoted 579 lines

-----Original Message-----
From: Eric Biggers <[email protected]>
Sent: Tuesday, December 11, 2018 11:47 PM
To: Parshuram Raju Thombare <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; akpm@linux- foundation.org; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; Alan Douglas <[email protected]>; Janek Kotas <[email protected]>; Rafal Ciepiela <[email protected]>; AnilKumar Chimata <[email protected]>; Ladvine D Almeida <[email protected]>; Satya Tangirala <[email protected]>; Paul Crowley <[email protected]>
Subject: Re: [PATCH 2/2] scsi: ufs: add inline crypto support to UFS HCD

EXTERNAL MAIL

[+Cc other people who have been working on this]

Hi Parshuram,

Quoted 16 lines · Parshuram Thombare

Add real time crypto support to UFS HCD using new device

mapper 'crypto-ufs'. dmsetup tool can be used to enable

real time / inline crypto support using device mapper

'crypt-ufs'.

---

 MAINTAINERS                      |    7 +
Quoted 7 lines
 block/Kconfig                    |    5 +
 drivers/scsi/ufs/Kconfig         |   12 +
 drivers/scsi/ufs/Makefile        |    1 +
 drivers/scsi/ufs/ufshcd-crypto.c |  453

++++++++++++++++++++++++++++++++++++++

Quoted 437 lines
 drivers/scsi/ufs/ufshcd-crypto.h |  102 +++++++++
 drivers/scsi/ufs/ufshcd.c        |   27 +++-
 drivers/scsi/ufs/ufshcd.h        |    6 +
 drivers/scsi/ufs/ufshci.h        |    1 +
 9 files changed, 613 insertions(+), 1 deletions(-)
 create mode 100644 drivers/scsi/ufs/ufshcd-crypto.c
 create mode 100644 drivers/scsi/ufs/ufshcd-crypto.h
MAINTAINERS +0 -0

index f485597..3a68126 100644

--- a/MAINTAINERS

+++ b/MAINTAINERS

MAINTAINERS +7 -0
@@ -15340,6 +15340,13 @@ S:	Supported
 
 F:	Documentation/scsi/ufs.txt
 
 F:	drivers/scsi/ufs/
 
 
 
+UNIVERSAL FLASH STORAGE HOST CONTROLLER CRYPTO DRIVER
 
+M:	Parshuram Thombare <[email protected]>
 
+L:	[email protected]
 
+S:	Supported
 
+F:	drivers/scsi/ufs/ufshcd-crypto.c
 
+F:	drivers/scsi/ufs/ufshcd-crypto.h
 
+
 
 UNIVERSAL FLASH STORAGE HOST CONTROLLER DRIVER DWC HOOKS
 
 M:	Joao Pinto <[email protected]>
 
 L:	[email protected]
block/Kconfig +0 -0

index f7045aa..6afe131 100644

--- a/block/Kconfig

+++ b/block/Kconfig

block/Kconfig +5 -0
@@ -224,4 +224,9 @@ config BLK_MQ_RDMA
 
 config BLK_PM
 
 	def_bool BLOCK && PM
 
 
 
+config BLK_DEV_HW_RT_ENCRYPTION
 
+	bool
 
+	depends on SCSI_UFSHCD_RT_ENCRYPTION
 
+	default n
 
+
 
 source block/Kconfig.iosched
drivers/scsi/ufs/Kconfig +0 -0

index 2ddbb26..09a3ec0 100644

--- a/drivers/scsi/ufs/Kconfig

+++ b/drivers/scsi/ufs/Kconfig

drivers/scsi/ufs/Kconfig +12 -0
@@ -136,3 +136,15 @@ config SCSI_UFS_BSG
 
 
 
 	  Select this if you need a bsg device node for your UFS controller.
 
 	  If unsure, say N.
 
+
 
+config SCSI_UFSHCD_RT_ENCRYPTION
 
+	bool "Universal Flash Storage Controller RT encryption support"
 
+	depends on SCSI_UFSHCD
 
+	default n
 
+	select BLK_DEV_HW_RT_ENCRYPTION if SCSI_UFSHCD_RT_ENCRYPTION
 
+	select BLK_DEV_DM if SCSI_UFSHCD_RT_ENCRYPTION
 
+	help
 
+	Universal Flash Storage Controller RT encryption support
 
+
 
+	Select this if you want to enable real time encryption on UFS controller
 
+	If unsure, say N.
drivers/scsi/ufs/Makefile +0 -0

index a3bd70c..6169096 100644

--- a/drivers/scsi/ufs/Makefile

+++ b/drivers/scsi/ufs/Makefile

drivers/scsi/ufs/Makefile +1 -0
@@ -7,6 +7,7 @@ obj-$(CONFIG_SCSI_UFS_QCOM) += ufs-qcom.o
 
 obj-$(CONFIG_SCSI_UFSHCD) += ufshcd-core.o
 
 ufshcd-core-y				+= ufshcd.o ufs-sysfs.o
 
 ufshcd-core-$(CONFIG_SCSI_UFS_BSG)	+= ufs_bsg.o
 
+ufshcd-core-$(CONFIG_SCSI_UFSHCD_RT_ENCRYPTION) += ufshcd-crypto.o
 
 obj-$(CONFIG_SCSI_UFSHCD_PCI) += ufshcd-pci.o
 
 obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o
 
 obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o
drivers/scsi/ufs/ufshcd-crypto.c +0 -0

new file mode 100644

index 0000000..9c95ff3

--- /dev/null

+++ b/drivers/scsi/ufs/ufshcd-crypto.c

drivers/scsi/ufs/ufshcd-crypto.c +141 -0
@@ -0,0 +1,453 @@
 
+// SPDX-License-Identifier: GPL-2.0
 
+/*
 
+ * UFS Host controller crypto driver
 
+ *
 
+ * Copyright (C) 2018 Cadence Design Systems, Inc.
 
+ *
 
+ * Authors:
 
+ *	Parshuram Thombare <[email protected]>
 
+ *
 
+ */
 
+
 
+#include <linux/module.h>
 
+#include <linux/kernel.h>
 
+#include <crypto/aes.h>
 
+#include <linux/device-mapper.h>
 
+#include "ufshcd.h"
 
+#include "ufshcd-crypto.h"
 
+#include "scsi/scsi_device.h"
 
+#include "scsi/scsi_host.h"
 
+
 
+struct ufshcd_dm_ctx {
 
+	struct dm_dev *dev;
 
+	sector_t start;
 
+	unsigned short int sector_size;
 
+	unsigned char sector_shift;
 
+	int cci;
 
+	int cap_idx;
 
+	char key[AES_MAX_KEY_SIZE];
 
+	struct ufs_hba *hba;
 
+};
 
+
 
+static int dm_registered;
 
+
 
+static inline int
 
+ufshcd_key_id_to_len(int key_id)
 
+{
 
+	int key_len = -1;
 
+
 
+	switch (key_id) {
 
+	case UFS_CRYPTO_KEY_ID_128BITS:
 
+		key_len = AES_KEYSIZE_128;
 
+		break;
 
+	case UFS_CRYPTO_KEY_ID_192BITS:
 
+		key_len = AES_KEYSIZE_192;
 
+		break;
 
+	case UFS_CRYPTO_KEY_ID_256BITS:
 
+		key_len = AES_KEYSIZE_256;
 
+		break;
 
+	default:
 
+		break;
 
+	}
 
+	return key_len;
 
+}
 
+
 
+static inline int
 
+ufshcd_key_len_to_id(int key_len)
 
+{
 
+	int key_id = -1;
 
+
 
+	switch (key_len) {
 
+	case AES_KEYSIZE_128:
 
+		key_id = UFS_CRYPTO_KEY_ID_128BITS;
 
+		break;
 
+	case AES_KEYSIZE_192:
 
+		key_id = UFS_CRYPTO_KEY_ID_192BITS;
 
+		break;
 
+	case AES_KEYSIZE_256:
 
+		key_id = UFS_CRYPTO_KEY_ID_256BITS;
 
+		break;
 
+	default:
 
+		break;
 
+	}
 
+	return key_id;
 
+}
 
+
 
+static void
 
+ufshcd_read_crypto_capabilities(struct ufs_hba *hba)
 
+{
 
+	u32 tmp, i;
 
+	struct ufshcd_crypto_ctx *cctx = hba->cctx;
 
+
 
+	for (i = 0; i < cctx->cap_cnt; i++) {
 
+		tmp = ufshcd_readl(hba, REG_UFS_CRYPTOCAP + i);
 
+		cctx->ccaps[i].key_id = (tmp & CRYPTO_CAPS_KS_MASK) >>
 
+						CRYPTO_CAPS_KS_SHIFT;
 
+		cctx->ccaps[i].sdusb = (tmp & CRYPTO_CAPS_SDUSB_MASK) >>
 
+						CRYPTO_CAPS_SDUSB_SHIFT;
 
+		cctx->ccaps[i].alg_id = (tmp & CRYPTO_CAPS_ALG_ID_MASK) >>
 
+						CRYPTO_CAPS_ALG_ID_SHIFT;
 
+	}
 
+}
 
+
 
+static inline int
 
+ufshcd_get_cap_idx(struct ufshcd_crypto_ctx *cctx, int alg_id,
 
+	int key_id)
 
+{
 
+	int cap_idx;
 
+
 
+	for (cap_idx = 0; cap_idx < cctx->cap_cnt; cap_idx++) {
 
+		if (((cctx->ccaps[cap_idx].alg_id == alg_id) &&
 
+			cctx->ccaps[cap_idx].key_id == key_id))
 
+			break;
 
+	}
 
+	return ((cap_idx < cctx->cap_cnt) ? cap_idx : -1);
 
+}
 
+
 
+static inline int
 
+ufshcd_get_cci_slot(struct ufshcd_crypto_ctx *cctx)
 
+{
 
+	int  cci;
 
+
 
+	for (cci = 0; cci < cctx->config_cnt; cci++) {
 
+		if (!cctx->cconfigs[cci].cfge) {
 
+			cctx->cconfigs[cci].cfge = 1;
 
+			break;
 
+		}
 
+	}
 
+	return ((cci < cctx->config_cnt) ? cci : -1);
 
+}
 
+
 
+static void
 
+ufshcd_aes_ecb_set_key(struct ufshcd_dm_ctx *ctx)
 
+{
 
+	int i, key_size;
 
+	u32 val, cconfig16, crypto_config_addr;
 
+	struct ufshcd_crypto_ctx *cctx;
 
+	struct ufshcd_crypto_config *cconfig;
 
+	struct ufshcd_crypto_cap ccap;
 
+
 
+	cctx = ctx->hba->cctx;
 
+	if (ctx->cci <= 0)
 
+		ctx->cci = ufshcd_get_cci_slot(cctx);
 
+	/* If no slot is available, slot 0 is shared */
 
+	ctx->cci = ctx->cci < 0 ? 0 : ctx->cci;
 
+	cconfig = &(cctx->cconfigs[ctx->cci]);
 
+	ccap = cctx->ccaps[ctx->cap_idx];
 
+	key_size = ufshcd_key_id_to_len(ccap.key_id);
 
+
 
+	if ((cconfig->cap_idx != ctx->cap_idx) ||
 
+		((key_size > 0) &&
 
+		memcmp(cconfig->key, ctx->key, key_size))) {

drivers/scsi/ufs/ufshcd-crypto.c

Quoted 10 lines
drivers/scsi/ufs/ufshcd-crypto.c +5 -0
 
+		cconfig->cap_idx = ctx->cap_idx;
 
+		memcpy(cconfig->key, ctx->key, key_size);
 
+		crypto_config_addr = cctx->crypto_config_base_addr +
 
+				ctx->cci * CRYPTO_CONFIG_SIZE;
 
+		cconfig16 = ccap.sdusb | (1 <<

CRYPTO_CCONFIG16_CFGE_SHIFT);

drivers/scsi/ufs/ufshcd-crypto.c

Quoted 1 line
drivers/scsi/ufs/ufshcd-crypto.c +1 -0
+		cconfig16 |= ((ctx->cap_idx <<

CRYPTO_CCONFIG16_CAP_IDX_SHIFT) &

Quoted 19 lines
drivers/scsi/ufs/ufshcd-crypto.c +10 -0
+				CRYPTO_CCONFIG16_CAP_IDX_MASK);
 
+		spin_lock(&cctx->crypto_lock);
 
+		for (i = 0; i < key_size; i += 4) {
 
+			val = (ctx->key[i] | (ctx->key[i + 1] << 8) |
 
+				(ctx->key[i + 2] << 16) |
 
+				(ctx->key[i + 3] << 24));
 
+			ufshcd_writel(ctx->hba, val, crypto_config_addr + i);
 
+		}
 
+		ufshcd_writel(ctx->hba, cpu_to_le32(cconfig16),
 
+				crypto_config_addr + (4 * 16));

drivers/scsi/ufs/ufshcd-crypto.c line 162

Quoted 10 lines
drivers/scsi/ufs/ufshcd-crypto.c +5 -0
 
+		spin_unlock(&cctx->crypto_lock);
 
+		/* Make sure keys are programmed */
 
+		mb();
 
+	}
 
+}

First of all, thanks for working on this. A lot of Android device vendors would

like to have upstream support for inline encryption. However, are you aware of

previous (unsuccessful) patchsets by other people working on this? Have you

addressed the concerns and improved on their work, or is this just yet another

new effort starting from scratch?

AnilKumar Chimata <[email protected]> (Qualcomm) in October 2018:

https://urldefense.proofpoint.com/v2/url?u=https-
3A__patchwork.kernel.org_cover_10645739_&d=DwIBAg&c=aUq983L2pue2FqKF oP6PGHMJQyoJ7kl3s3GZ-_haXqY&r=GTefrem3hiBCnsjCOqAuapQHRN8- rKC1FRbk0it- LDs&m=L9VLjsZ31dZ4TP4LdveuvcjPFzdZWGlZaZnzqGZH3zc&s=ZzYaAaVic5TB4RUS cR5kzcM_8gvLYdlNAzuY80_ASzI&e=

Ladvine D Almeida <[email protected]> in May 2018:

	https://urldefense.proofpoint.com/v2/url?u=http-3A__lists-

2Darchives.com_linux-2Dkernel_29135393-2Dscsi-2Dufs-2Dufs-2Dhost- 2Dcontroller-2Dcrypto- 2Dchanges.html&d=DwIBAg&c=aUq983L2pue2FqKFoP6PGHMJQyoJ7kl3s3GZ- _haXqY&r=GTefrem3hiBCnsjCOqAuapQHRN8-rKC1FRbk0it- LDs&m=L9VLjsZ31dZ4TP4LdveuvcjPFzdZWGlZaZnzqGZH3zc&s=3pxSBZrt_DpDSz- ZXrM7_bj0QXmRzcbasPl_wB259Us&e=

Satya Tangirala <[email protected]> is also working on it but I don't believe

Quoted 7 lines

he's sent out a patchset yet.

It would be nice to coordinate to get a proper solution upstream that works for

everyone, rather than having everyone try independently and fail repeatedly :-)

I had look at Ladvine's submission and think the approach of using Linux crypto API and adding algorithm which is supposed to work inline (and with UFS devices only) in global pool of algorithms (which is supposed to be generic) makes it risky, if selected/used for other type of device not supporting inline encryption. Also apparently it is not possible to support multiple UFS controllers in the system with that approach.
There was suggestion from Milan to use separate device mapper which seems cleaner way of enabling inline encryption. Hence new device mapper is used.
I think this is better idea to coordinate and come up with a generic solution.

Quoted 8 lines

Also, note that ECB mode is not appropriate for disk encryption. So this patch

(and the hardware you tested it on, if that's all it supports) is effectively

useless as-is. You need to support XTS mode.

For now only AES-ECB is supported, we are working on adding other modes.

Quoted 8 lines

Thanks!

- Eric

Regards,
Parshuram Thombare

#6 lore raw
Quoted 24 lines · Parshuram Raju Thombare

Hello Eric,

Thank you for a comment.

Quoted 20 lines

-----Original Message-----
From: Eric Biggers <[email protected]>
Sent: Tuesday, December 11, 2018 11:47 PM
To: Parshuram Raju Thombare <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; akpm@linux- foundation.org; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; Alan Douglas <[email protected]>; Janek Kotas <[email protected]>; Rafal Ciepiela <[email protected]>; AnilKumar Chimata <[email protected]>; Ladvine D Almeida <[email protected]>; Satya Tangirala <[email protected]>; Paul Crowley <[email protected]>
Subject: Re: [PATCH 2/2] scsi: ufs: add inline crypto support to UFS HCD

EXTERNAL MAIL

[+Cc other people who have been working on this]

Eric, Thanks for cc-ing me to the mail chain.

Parshuram,
Glad to know that you are working on the Inline Encryption support. My concerns are mentioned inline below.

Quoted 9 lines
Quoted 9 lines

Hi Parshuram,

On Tue, Dec 11, 2018 at 09:50:27AM +0000, Parshuram Thombare wrote:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Add real time crypto support to UFS HCD using new device

mapper 'crypto-ufs'. dmsetup tool can be used to enable

real time / inline crypto support using device mapper

'crypt-ufs'.

Where is Crypto target 'crypto-ufs' implementation available? Did you submitted any other patch for the same?
Also, it is better to provide a generic name as the target is valid for all other block devices.

Quoted 272 lines
Quoted 272 lines
Quoted 9 lines

---

 MAINTAINERS                      |    7 +
Quoted 7 lines
 block/Kconfig                    |    5 +
 drivers/scsi/ufs/Kconfig         |   12 +
 drivers/scsi/ufs/Makefile        |    1 +
 drivers/scsi/ufs/ufshcd-crypto.c |  453

++++++++++++++++++++++++++++++++++++++

Quoted 253 lines
 drivers/scsi/ufs/ufshcd-crypto.h |  102 +++++++++
 drivers/scsi/ufs/ufshcd.c        |   27 +++-
 drivers/scsi/ufs/ufshcd.h        |    6 +
 drivers/scsi/ufs/ufshci.h        |    1 +
 9 files changed, 613 insertions(+), 1 deletions(-)
 create mode 100644 drivers/scsi/ufs/ufshcd-crypto.c
 create mode 100644 drivers/scsi/ufs/ufshcd-crypto.h
MAINTAINERS +0 -0

index f485597..3a68126 100644

--- a/MAINTAINERS

+++ b/MAINTAINERS

MAINTAINERS +7 -0
@@ -15340,6 +15340,13 @@ S:	Supported
 
 F:	Documentation/scsi/ufs.txt
 
 F:	drivers/scsi/ufs/
 
 
 
+UNIVERSAL FLASH STORAGE HOST CONTROLLER CRYPTO DRIVER
 
+M:	Parshuram Thombare <[email protected]>
 
+L:	[email protected]
 
+S:	Supported
 
+F:	drivers/scsi/ufs/ufshcd-crypto.c
 
+F:	drivers/scsi/ufs/ufshcd-crypto.h
 
+
 
 UNIVERSAL FLASH STORAGE HOST CONTROLLER DRIVER DWC HOOKS
 
 M:	Joao Pinto <[email protected]>
 
 L:	[email protected]
block/Kconfig +0 -0

index f7045aa..6afe131 100644

--- a/block/Kconfig

+++ b/block/Kconfig

block/Kconfig +5 -0
@@ -224,4 +224,9 @@ config BLK_MQ_RDMA
 
 config BLK_PM
 
 	def_bool BLOCK && PM
 
 
 
+config BLK_DEV_HW_RT_ENCRYPTION
 
+	bool
 
+	depends on SCSI_UFSHCD_RT_ENCRYPTION
 
+	default n
 
+
 
 source block/Kconfig.iosched
drivers/scsi/ufs/Kconfig +0 -0

index 2ddbb26..09a3ec0 100644

--- a/drivers/scsi/ufs/Kconfig

+++ b/drivers/scsi/ufs/Kconfig

drivers/scsi/ufs/Kconfig +12 -0
@@ -136,3 +136,15 @@ config SCSI_UFS_BSG
 
 
 
 	  Select this if you need a bsg device node for your UFS controller.
 
 	  If unsure, say N.
 
+
 
+config SCSI_UFSHCD_RT_ENCRYPTION
 
+	bool "Universal Flash Storage Controller RT encryption support"
 
+	depends on SCSI_UFSHCD
 
+	default n
 
+	select BLK_DEV_HW_RT_ENCRYPTION if SCSI_UFSHCD_RT_ENCRYPTION
 
+	select BLK_DEV_DM if SCSI_UFSHCD_RT_ENCRYPTION
 
+	help
 
+	Universal Flash Storage Controller RT encryption support
 
+
 
+	Select this if you want to enable real time encryption on UFS controller
 
+	If unsure, say N.
drivers/scsi/ufs/Makefile +0 -0

index a3bd70c..6169096 100644

--- a/drivers/scsi/ufs/Makefile

+++ b/drivers/scsi/ufs/Makefile

drivers/scsi/ufs/Makefile +1 -0
@@ -7,6 +7,7 @@ obj-$(CONFIG_SCSI_UFS_QCOM) += ufs-qcom.o
 
 obj-$(CONFIG_SCSI_UFSHCD) += ufshcd-core.o
 
 ufshcd-core-y				+= ufshcd.o ufs-sysfs.o
 
 ufshcd-core-$(CONFIG_SCSI_UFS_BSG)	+= ufs_bsg.o
 
+ufshcd-core-$(CONFIG_SCSI_UFSHCD_RT_ENCRYPTION) += ufshcd-crypto.o
 
 obj-$(CONFIG_SCSI_UFSHCD_PCI) += ufshcd-pci.o
 
 obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o
 
 obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o
drivers/scsi/ufs/ufshcd-crypto.c +0 -0

new file mode 100644

index 0000000..9c95ff3

--- /dev/null

+++ b/drivers/scsi/ufs/ufshcd-crypto.c

drivers/scsi/ufs/ufshcd-crypto.c +49 -0
@@ -0,0 +1,453 @@
 
+// SPDX-License-Identifier: GPL-2.0
 
+/*
 
+ * UFS Host controller crypto driver
 
+ *
 
+ * Copyright (C) 2018 Cadence Design Systems, Inc.
 
+ *
 
+ * Authors:
 
+ *	Parshuram Thombare <[email protected]>
 
+ *
 
+ */
 
+
 
+#include <linux/module.h>
 
+#include <linux/kernel.h>
 
+#include <crypto/aes.h>
 
+#include <linux/device-mapper.h>
 
+#include "ufshcd.h"
 
+#include "ufshcd-crypto.h"
 
+#include "scsi/scsi_device.h"
 
+#include "scsi/scsi_host.h"
 
+
 
+struct ufshcd_dm_ctx {
 
+	struct dm_dev *dev;
 
+	sector_t start;
 
+	unsigned short int sector_size;
 
+	unsigned char sector_shift;
 
+	int cci;
 
+	int cap_idx;
 
+	char key[AES_MAX_KEY_SIZE];
 
+	struct ufs_hba *hba;
 
+};
 
+
 
+static int dm_registered;
 
+
 
+static inline int
 
+ufshcd_key_id_to_len(int key_id)
 
+{
 
+	int key_len = -1;
 
+
 
+	switch (key_id) {
 
+	case UFS_CRYPTO_KEY_ID_128BITS:
 
+		key_len = AES_KEYSIZE_128;
 
+		break;
 
+	case UFS_CRYPTO_KEY_ID_192BITS:
 
+		key_len = AES_KEYSIZE_192;
 
+		break;
 
+	case UFS_CRYPTO_KEY_ID_256BITS:
 
+		key_len = AES_KEYSIZE_256;
 
+		break;
 
+	default:
Quoted 7 lines
Quoted 7 lines

drivers/scsi/ufs/ufshcd-crypto.c line 53

Quoted 7 lines
drivers/scsi/ufs/ufshcd-crypto.c +4 -0
+		break;
 
+	}
 
+	return key_len;
 
+}

Why not -EINVAL for invalid key length?

Quoted 271 lines
Quoted 264 lines
Quoted 176 lines
drivers/scsi/ufs/ufshcd-crypto.c +88 -0
+
 
+static inline int
 
+ufshcd_key_len_to_id(int key_len)
 
+{
 
+	int key_id = -1;
 
+
 
+	switch (key_len) {
 
+	case AES_KEYSIZE_128:
 
+		key_id = UFS_CRYPTO_KEY_ID_128BITS;
 
+		break;
 
+	case AES_KEYSIZE_192:
 
+		key_id = UFS_CRYPTO_KEY_ID_192BITS;
 
+		break;
 
+	case AES_KEYSIZE_256:
 
+		key_id = UFS_CRYPTO_KEY_ID_256BITS;
 
+		break;
 
+	default:
 
+		break;
 
+	}
 
+	return key_id;
 
+}
 
+
 
+static void
 
+ufshcd_read_crypto_capabilities(struct ufs_hba *hba)
 
+{
 
+	u32 tmp, i;
 
+	struct ufshcd_crypto_ctx *cctx = hba->cctx;
 
+
 
+	for (i = 0; i < cctx->cap_cnt; i++) {
 
+		tmp = ufshcd_readl(hba, REG_UFS_CRYPTOCAP + i);
 
+		cctx->ccaps[i].key_id = (tmp & CRYPTO_CAPS_KS_MASK) >>
 
+						CRYPTO_CAPS_KS_SHIFT;
 
+		cctx->ccaps[i].sdusb = (tmp & CRYPTO_CAPS_SDUSB_MASK) >>
 
+						CRYPTO_CAPS_SDUSB_SHIFT;
 
+		cctx->ccaps[i].alg_id = (tmp & CRYPTO_CAPS_ALG_ID_MASK) >>
 
+						CRYPTO_CAPS_ALG_ID_SHIFT;
 
+	}
 
+}
 
+
 
+static inline int
 
+ufshcd_get_cap_idx(struct ufshcd_crypto_ctx *cctx, int alg_id,
 
+	int key_id)
 
+{
 
+	int cap_idx;
 
+
 
+	for (cap_idx = 0; cap_idx < cctx->cap_cnt; cap_idx++) {
 
+		if (((cctx->ccaps[cap_idx].alg_id == alg_id) &&
 
+			cctx->ccaps[cap_idx].key_id == key_id))
 
+			break;
 
+	}
 
+	return ((cap_idx < cctx->cap_cnt) ? cap_idx : -1);
 
+}
 
+
 
+static inline int
 
+ufshcd_get_cci_slot(struct ufshcd_crypto_ctx *cctx)
 
+{
 
+	int  cci;
 
+
 
+	for (cci = 0; cci < cctx->config_cnt; cci++) {
 
+		if (!cctx->cconfigs[cci].cfge) {
 
+			cctx->cconfigs[cci].cfge = 1;
 
+			break;
 
+		}
 
+	}
 
+	return ((cci < cctx->config_cnt) ? cci : -1);
 
+}
 
+
 
+static void
 
+ufshcd_aes_ecb_set_key(struct ufshcd_dm_ctx *ctx)
 
+{
 
+	int i, key_size;
 
+	u32 val, cconfig16, crypto_config_addr;
 
+	struct ufshcd_crypto_ctx *cctx;
 
+	struct ufshcd_crypto_config *cconfig;
 
+	struct ufshcd_crypto_cap ccap;
 
+
 
+	cctx = ctx->hba->cctx;
 
+	if (ctx->cci <= 0)
 
+		ctx->cci = ufshcd_get_cci_slot(cctx);
 
+	/* If no slot is available, slot 0 is shared */
 
+	ctx->cci = ctx->cci < 0 ? 0 : ctx->cci;
 
+	cconfig = &(cctx->cconfigs[ctx->cci]);
 
+	ccap = cctx->ccaps[ctx->cap_idx];
 
+	key_size = ufshcd_key_id_to_len(ccap.key_id);
 
+
 
+	if ((cconfig->cap_idx != ctx->cap_idx) ||
 
+		((key_size > 0) &&
 
+		memcmp(cconfig->key, ctx->key, key_size))) {

drivers/scsi/ufs/ufshcd-crypto.c

Quoted 10 lines
drivers/scsi/ufs/ufshcd-crypto.c +5 -0
 
+		cconfig->cap_idx = ctx->cap_idx;
 
+		memcpy(cconfig->key, ctx->key, key_size);
 
+		crypto_config_addr = cctx->crypto_config_base_addr +
 
+				ctx->cci * CRYPTO_CONFIG_SIZE;
 
+		cconfig16 = ccap.sdusb | (1 <<

CRYPTO_CCONFIG16_CFGE_SHIFT);

drivers/scsi/ufs/ufshcd-crypto.c

Quoted 1 line
drivers/scsi/ufs/ufshcd-crypto.c +1 -0
+		cconfig16 |= ((ctx->cap_idx <<

CRYPTO_CCONFIG16_CAP_IDX_SHIFT) &

Quoted 19 lines
drivers/scsi/ufs/ufshcd-crypto.c +10 -0
+				CRYPTO_CCONFIG16_CAP_IDX_MASK);
 
+		spin_lock(&cctx->crypto_lock);
 
+		for (i = 0; i < key_size; i += 4) {
 
+			val = (ctx->key[i] | (ctx->key[i + 1] << 8) |
 
+				(ctx->key[i + 2] << 16) |
 
+				(ctx->key[i + 3] << 24));
 
+			ufshcd_writel(ctx->hba, val, crypto_config_addr + i);
 
+		}
 
+		ufshcd_writel(ctx->hba, cpu_to_le32(cconfig16),
 
+				crypto_config_addr + (4 * 16));

drivers/scsi/ufs/ufshcd-crypto.c line 162

Quoted 10 lines
drivers/scsi/ufs/ufshcd-crypto.c +5 -0
 
+		spin_unlock(&cctx->crypto_lock);
 
+		/* Make sure keys are programmed */
 
+		mb();
 
+	}
 
+}

First of all, thanks for working on this. A lot of Android device vendors would

like to have upstream support for inline encryption. However, are you aware of

previous (unsuccessful) patchsets by other people working on this? Have you

addressed the concerns and improved on their work, or is this just yet another

new effort starting from scratch?

AnilKumar Chimata <[email protected]> (Qualcomm) in October 2018:

https://urldefense.proofpoint.com/v2/url?u=https-
3A__patchwork.kernel.org_cover_10645739_&d=DwIBAg&c=aUq983L2pue2FqKF oP6PGHMJQyoJ7kl3s3GZ-_haXqY&r=GTefrem3hiBCnsjCOqAuapQHRN8- rKC1FRbk0it- LDs&m=L9VLjsZ31dZ4TP4LdveuvcjPFzdZWGlZaZnzqGZH3zc&s=ZzYaAaVic5TB4RUS cR5kzcM_8gvLYdlNAzuY80_ASzI&e=

Ladvine D Almeida <[email protected]> in May 2018:

	https://urldefense.proofpoint.com/v2/url?u=http-3A__lists-

2Darchives.com_linux-2Dkernel_29135393-2Dscsi-2Dufs-2Dufs-2Dhost- 2Dcontroller-2Dcrypto- 2Dchanges.html&d=DwIBAg&c=aUq983L2pue2FqKFoP6PGHMJQyoJ7kl3s3GZ- _haXqY&r=GTefrem3hiBCnsjCOqAuapQHRN8-rKC1FRbk0it- LDs&m=L9VLjsZ31dZ4TP4LdveuvcjPFzdZWGlZaZnzqGZH3zc&s=3pxSBZrt_DpDSz- ZXrM7_bj0QXmRzcbasPl_wB259Us&e=

Satya Tangirala <[email protected]> is also working on it but I don't believe

Quoted 7 lines

he's sent out a patchset yet.

It would be nice to coordinate to get a proper solution upstream that works for

everyone, rather than having everyone try independently and fail repeatedly :-)

Quoted 8 lines

I had look at Ladvine's submission and think the approach of using Linux crypto API and adding algorithm which is supposed to work inline (and with UFS devices only) in global pool of algorithms (which is supposed to be generic) makes it risky, if selected/used for other type of device not supporting inline encryption. Also apparently it is not possible to support multiple UFS controllers in the system with that approach.
There was suggestion from Milan to use separate device mapper which seems cleaner way of enabling inline encryption. Hence new device mapper is used.
I think this is better idea to coordinate and come up with a generic solution.

Suggest to take a look into the article https://lwn.net/Articles/717754
My real concern is how to achieve it without any modifications to the bio.(because key slot information has to finally reach the target block device)

Quoted 12 lines
Quoted 8 lines

Also, note that ECB mode is not appropriate for disk encryption. So this patch

(and the hardware you tested it on, if that's all it supports) is effectively

useless as-is. You need to support XTS mode.

For now only AES-ECB is supported, we are working on adding other modes.

Quoted 3 lines
Quoted 9 lines
Quoted 5 lines

Thanks!

- Eric

Regards,
Parshuram Thombare

Thanks,
Ladvine

maintainer
#7 lore raw
Quoted 4 lines · Ladvine D Almeida

Suggest to take a look into the article https://lwn.net/Articles/717754
My real concern is how to achieve it without any modifications to the bio.(because key slot information has to finally reach the target block device)

Guys, you both need to edit when you reply, wading through 650 lines of text to get to this...

You obviously can't modify the bio if you don't own it, but you could clone it and then you have storage in ->bi_private.

Signature · 1 line
Jens Axboe
#8 lore raw
Quoted 8 lines · Jens Axboe
Quoted 4 lines · Ladvine D Almeida

Suggest to take a look into the article https://urldefense.proofpoint.com/v2/url?u=https-3A__lwn.net_Articles_717754&d=DwICaQ&c=DPL6_X_6JkXFx7AXWqB0tg&r=z00zRD9ARrwHpe-XSl1OtUp1uNKGYoXI1G2DhOaDDBI&m=-pGzV3wdSje337vKOYoYB6NgU_DGDmQvfQ9egLDRYNQ&s=c4FXsrwD0BLAjE4UC47R2ngLnx_DP5jiOr0dtZ7tGsw&e=
My real concern is how to achieve it without any modifications to the
bio.(because key slot information has to finally reach the target block
device)

Guys, you both need to edit when you reply, wading through 650 lines of text to get to this...

Jens, Thanks for your reply. I will take care of it next time onwards. :-)

Quoted 4 lines

You obviously can't modify the bio if you don't own it, but you could clone it and then you have storage in ->bi_private.

I agree. I can clone the bio in the crypto target and use bi_private. But, when i finally submit the bio and it reaches the target block device, the information stored in bi_private is lost.

Regards,
Ladvine

maintainer
#9 lore raw
Quoted 12 lines · Ladvine D Almeida
Quoted 8 lines · Jens Axboe
Quoted 4 lines · Ladvine D Almeida

Suggest to take a look into the article https://urldefense.proofpoint.com/v2/url?u=https-3A__lwn.net_Articles_717754&d=DwICaQ&c=DPL6_X_6JkXFx7AXWqB0tg&r=z00zRD9ARrwHpe-XSl1OtUp1uNKGYoXI1G2DhOaDDBI&m=-pGzV3wdSje337vKOYoYB6NgU_DGDmQvfQ9egLDRYNQ&s=c4FXsrwD0BLAjE4UC47R2ngLnx_DP5jiOr0dtZ7tGsw&e=
My real concern is how to achieve it without any modifications to the
bio.(because key slot information has to finally reach the target block
device)

Guys, you both need to edit when you reply, wading through 650 lines of text to get to this...

Jens, Thanks for your reply. I will take care of it next time onwards. :-)

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

You obviously can't modify the bio if you don't own it, but you could clone it and then you have storage in ->bi_private.

I agree. I can clone the bio in the crypto target and use bi_private. But, when i finally submit the bio and it reaches the target block device, the information stored in bi_private is lost.

I don't follow, what do you mean it's lost? It's persistent from bio setup to end_io time.

Signature · 1 line
Jens Axboe
#10 lore raw
In reply to Ladvine D Almeida

Hi Ladvine,

Quoted 7 lines

From: Ladvine D Almeida <[email protected]>
Sent: Friday, December 14, 2018 1:10 AM
Subject: Re: [PATCH 2/2] scsi: ufs: add inline crypto support to UFS HCD Where is Crypto target 'crypto-ufs' implementation available? Did you submitted any other patch for the same?
Also, it is better to provide a generic name as the target is valid for all other block devices.

[PATCH 2/2] have crypto-ufs implementation. [PATCH 1/2] add variable to struct bio. Device mapper name is not generic because there are ufs specific things but we should be able to make it generic.

Regards,
Parshuram Thombare

Quoted 690 lines

-----Original Message-----
From: Ladvine D Almeida <[email protected]>
Sent: Friday, December 14, 2018 1:10 AM
To: Parshuram Raju Thombare <[email protected]>; Eric Biggers <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; akpm@linux- foundation.org; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; Alan Douglas <[email protected]>; Janek Kotas <[email protected]>; Rafal Ciepiela <[email protected]>; AnilKumar Chimata <[email protected]>; Ladvine D Almeida <[email protected]>; Satya Tangirala <[email protected]>; Paul Crowley <[email protected]>; Manjunath M Bettegowda <[email protected]>; Tejas Joglekar <[email protected]>; Joao Pinto <[email protected]>; linux- [email protected]
Subject: Re: [PATCH 2/2] scsi: ufs: add inline crypto support to UFS HCD

EXTERNAL MAIL

Quoted 25 lines · Parshuram Raju Thombare

Hello Eric,

Thank you for a comment.

Quoted 21 lines

-----Original Message-----
From: Eric Biggers <[email protected]>
Sent: Tuesday, December 11, 2018 11:47 PM
To: Parshuram Raju Thombare <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; akpm@linux- foundation.org; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; Alan Douglas <[email protected]>; Janek Kotas <[email protected]>; Rafal Ciepiela <[email protected]>; AnilKumar Chimata <[email protected]>; Ladvine D Almeida <[email protected]>; Satya Tangirala <[email protected]>; Paul Crowley <[email protected]>
Subject: Re: [PATCH 2/2] scsi: ufs: add inline crypto support to UFS HCD

EXTERNAL MAIL

[+Cc other people who have been working on this]

Eric, Thanks for cc-ing me to the mail chain.

Parshuram,
Glad to know that you are working on the Inline Encryption support. My concerns are mentioned inline below.

Quoted 9 lines
Quoted 9 lines

Hi Parshuram,

On Tue, Dec 11, 2018 at 09:50:27AM +0000, Parshuram Thombare wrote:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Add real time crypto support to UFS HCD using new device

mapper 'crypto-ufs'. dmsetup tool can be used to enable

real time / inline crypto support using device mapper

'crypt-ufs'.

Where is Crypto target 'crypto-ufs' implementation available? Did you submitted any other patch for the same?
Also, it is better to provide a generic name as the target is valid for all other block devices.

Quoted 274 lines
Quoted 274 lines
Quoted 9 lines

---

 MAINTAINERS                      |    7 +
Quoted 7 lines
 block/Kconfig                    |    5 +
 drivers/scsi/ufs/Kconfig         |   12 +
 drivers/scsi/ufs/Makefile        |    1 +
 drivers/scsi/ufs/ufshcd-crypto.c |  453

++++++++++++++++++++++++++++++++++++++

Quoted 255 lines
 drivers/scsi/ufs/ufshcd-crypto.h |  102 +++++++++
 drivers/scsi/ufs/ufshcd.c        |   27 +++-
 drivers/scsi/ufs/ufshcd.h        |    6 +
 drivers/scsi/ufs/ufshci.h        |    1 +
 9 files changed, 613 insertions(+), 1 deletions(-)
 create mode 100644 drivers/scsi/ufs/ufshcd-crypto.c
 create mode 100644 drivers/scsi/ufs/ufshcd-crypto.h
MAINTAINERS +0 -0

index f485597..3a68126 100644

--- a/MAINTAINERS

+++ b/MAINTAINERS

MAINTAINERS +7 -0
@@ -15340,6 +15340,13 @@ S:	Supported
 
 F:	Documentation/scsi/ufs.txt
 
 F:	drivers/scsi/ufs/
 
 
 
+UNIVERSAL FLASH STORAGE HOST CONTROLLER CRYPTO DRIVER
 
+M:	Parshuram Thombare <[email protected]>
 
+L:	[email protected]
 
+S:	Supported
 
+F:	drivers/scsi/ufs/ufshcd-crypto.c
 
+F:	drivers/scsi/ufs/ufshcd-crypto.h
 
+
 
 UNIVERSAL FLASH STORAGE HOST CONTROLLER DRIVER DWC HOOKS
 
 M:	Joao Pinto <[email protected]>
 
 L:	[email protected]
block/Kconfig +0 -0

index f7045aa..6afe131 100644

--- a/block/Kconfig

+++ b/block/Kconfig

block/Kconfig +5 -0
@@ -224,4 +224,9 @@ config BLK_MQ_RDMA
 
 config BLK_PM
 
 	def_bool BLOCK && PM
 
 
 
+config BLK_DEV_HW_RT_ENCRYPTION
 
+	bool
 
+	depends on SCSI_UFSHCD_RT_ENCRYPTION
 
+	default n
 
+
 
 source block/Kconfig.iosched
drivers/scsi/ufs/Kconfig +0 -0

index 2ddbb26..09a3ec0 100644

--- a/drivers/scsi/ufs/Kconfig

+++ b/drivers/scsi/ufs/Kconfig

drivers/scsi/ufs/Kconfig +13 -0
@@ -136,3 +136,15 @@ config SCSI_UFS_BSG
 
 
 
 	  Select this if you need a bsg device node for your UFS controller.
 
 	  If unsure, say N.
 
+
 
+config SCSI_UFSHCD_RT_ENCRYPTION
 
+	bool "Universal Flash Storage Controller RT encryption support"
 
+	depends on SCSI_UFSHCD
 
+	default n
 
+	select BLK_DEV_HW_RT_ENCRYPTION if SCSI_UFSHCD_RT_ENCRYPTION
 
+	select BLK_DEV_DM if SCSI_UFSHCD_RT_ENCRYPTION
 
+	help
 
+	Universal Flash Storage Controller RT encryption support
 
+
 
+	Select this if you want to enable real time encryption on UFS
+controller
 
+	If unsure, say N.
drivers/scsi/ufs/Makefile +0 -0

index a3bd70c..6169096 100644

--- a/drivers/scsi/ufs/Makefile

+++ b/drivers/scsi/ufs/Makefile

drivers/scsi/ufs/Makefile +1 -0
@@ -7,6 +7,7 @@ obj-$(CONFIG_SCSI_UFS_QCOM) += ufs-qcom.o
 
 obj-$(CONFIG_SCSI_UFSHCD) += ufshcd-core.o
 
 ufshcd-core-y				+= ufshcd.o ufs-sysfs.o
 
 ufshcd-core-$(CONFIG_SCSI_UFS_BSG)	+= ufs_bsg.o
 
+ufshcd-core-$(CONFIG_SCSI_UFSHCD_RT_ENCRYPTION) += ufshcd-crypto.o
 
 obj-$(CONFIG_SCSI_UFSHCD_PCI) += ufshcd-pci.o
 
 obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o
 
 obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o
drivers/scsi/ufs/ufshcd-crypto.c +0 -0

new file mode 100644

index 0000000..9c95ff3

--- /dev/null

+++ b/drivers/scsi/ufs/ufshcd-crypto.c

drivers/scsi/ufs/ufshcd-crypto.c +49 -0
@@ -0,0 +1,453 @@
 
+// SPDX-License-Identifier: GPL-2.0
 
+/*
 
+ * UFS Host controller crypto driver
 
+ *
 
+ * Copyright (C) 2018 Cadence Design Systems, Inc.
 
+ *
 
+ * Authors:
 
+ *	Parshuram Thombare <[email protected]>
 
+ *
 
+ */
 
+
 
+#include <linux/module.h>
 
+#include <linux/kernel.h>
 
+#include <crypto/aes.h>
 
+#include <linux/device-mapper.h>
 
+#include "ufshcd.h"
 
+#include "ufshcd-crypto.h"
 
+#include "scsi/scsi_device.h"
 
+#include "scsi/scsi_host.h"
 
+
 
+struct ufshcd_dm_ctx {
 
+	struct dm_dev *dev;
 
+	sector_t start;
 
+	unsigned short int sector_size;
 
+	unsigned char sector_shift;
 
+	int cci;
 
+	int cap_idx;
 
+	char key[AES_MAX_KEY_SIZE];
 
+	struct ufs_hba *hba;
 
+};
 
+
 
+static int dm_registered;
 
+
 
+static inline int
 
+ufshcd_key_id_to_len(int key_id)
 
+{
 
+	int key_len = -1;
 
+
 
+	switch (key_id) {
 
+	case UFS_CRYPTO_KEY_ID_128BITS:
 
+		key_len = AES_KEYSIZE_128;
 
+		break;
 
+	case UFS_CRYPTO_KEY_ID_192BITS:
 
+		key_len = AES_KEYSIZE_192;
 
+		break;
 
+	case UFS_CRYPTO_KEY_ID_256BITS:
 
+		key_len = AES_KEYSIZE_256;
 
+		break;
 
+	default:
Quoted 7 lines
Quoted 7 lines

drivers/scsi/ufs/ufshcd-crypto.c line 53

Quoted 7 lines
drivers/scsi/ufs/ufshcd-crypto.c +4 -0
+		break;
 
+	}
 
+	return key_len;
 
+}

Why not -EINVAL for invalid key length?

Quoted 239 lines
Quoted 239 lines
Quoted 176 lines
drivers/scsi/ufs/ufshcd-crypto.c +88 -0
+
 
+static inline int
 
+ufshcd_key_len_to_id(int key_len)
 
+{
 
+	int key_id = -1;
 
+
 
+	switch (key_len) {
 
+	case AES_KEYSIZE_128:
 
+		key_id = UFS_CRYPTO_KEY_ID_128BITS;
 
+		break;
 
+	case AES_KEYSIZE_192:
 
+		key_id = UFS_CRYPTO_KEY_ID_192BITS;
 
+		break;
 
+	case AES_KEYSIZE_256:
 
+		key_id = UFS_CRYPTO_KEY_ID_256BITS;
 
+		break;
 
+	default:
 
+		break;
 
+	}
 
+	return key_id;
 
+}
 
+
 
+static void
 
+ufshcd_read_crypto_capabilities(struct ufs_hba *hba)
 
+{
 
+	u32 tmp, i;
 
+	struct ufshcd_crypto_ctx *cctx = hba->cctx;
 
+
 
+	for (i = 0; i < cctx->cap_cnt; i++) {
 
+		tmp = ufshcd_readl(hba, REG_UFS_CRYPTOCAP + i);
 
+		cctx->ccaps[i].key_id = (tmp & CRYPTO_CAPS_KS_MASK) >>
 
+						CRYPTO_CAPS_KS_SHIFT;
 
+		cctx->ccaps[i].sdusb = (tmp & CRYPTO_CAPS_SDUSB_MASK) >>
 
+						CRYPTO_CAPS_SDUSB_SHIFT;
 
+		cctx->ccaps[i].alg_id = (tmp & CRYPTO_CAPS_ALG_ID_MASK) >>
 
+						CRYPTO_CAPS_ALG_ID_SHIFT;
 
+	}
 
+}
 
+
 
+static inline int
 
+ufshcd_get_cap_idx(struct ufshcd_crypto_ctx *cctx, int alg_id,
 
+	int key_id)
 
+{
 
+	int cap_idx;
 
+
 
+	for (cap_idx = 0; cap_idx < cctx->cap_cnt; cap_idx++) {
 
+		if (((cctx->ccaps[cap_idx].alg_id == alg_id) &&
 
+			cctx->ccaps[cap_idx].key_id == key_id))
 
+			break;
 
+	}
 
+	return ((cap_idx < cctx->cap_cnt) ? cap_idx : -1);
 
+}
 
+
 
+static inline int
 
+ufshcd_get_cci_slot(struct ufshcd_crypto_ctx *cctx)
 
+{
 
+	int  cci;
 
+
 
+	for (cci = 0; cci < cctx->config_cnt; cci++) {
 
+		if (!cctx->cconfigs[cci].cfge) {
 
+			cctx->cconfigs[cci].cfge = 1;
 
+			break;
 
+		}
 
+	}
 
+	return ((cci < cctx->config_cnt) ? cci : -1);
 
+}
 
+
 
+static void
 
+ufshcd_aes_ecb_set_key(struct ufshcd_dm_ctx *ctx)
 
+{
 
+	int i, key_size;
 
+	u32 val, cconfig16, crypto_config_addr;
 
+	struct ufshcd_crypto_ctx *cctx;
 
+	struct ufshcd_crypto_config *cconfig;
 
+	struct ufshcd_crypto_cap ccap;
 
+
 
+	cctx = ctx->hba->cctx;
 
+	if (ctx->cci <= 0)
 
+		ctx->cci = ufshcd_get_cci_slot(cctx);
 
+	/* If no slot is available, slot 0 is shared */
 
+	ctx->cci = ctx->cci < 0 ? 0 : ctx->cci;
 
+	cconfig = &(cctx->cconfigs[ctx->cci]);
 
+	ccap = cctx->ccaps[ctx->cap_idx];
 
+	key_size = ufshcd_key_id_to_len(ccap.key_id);
 
+
 
+	if ((cconfig->cap_idx != ctx->cap_idx) ||
 
+		((key_size > 0) &&
 
+		memcmp(cconfig->key, ctx->key, key_size))) {

drivers/scsi/ufs/ufshcd-crypto.c

Quoted 10 lines
drivers/scsi/ufs/ufshcd-crypto.c +5 -0
 
+		cconfig->cap_idx = ctx->cap_idx;
 
+		memcpy(cconfig->key, ctx->key, key_size);
 
+		crypto_config_addr = cctx->crypto_config_base_addr +
 
+				ctx->cci * CRYPTO_CONFIG_SIZE;
 
+		cconfig16 = ccap.sdusb | (1 <<

CRYPTO_CCONFIG16_CFGE_SHIFT);

drivers/scsi/ufs/ufshcd-crypto.c

Quoted 1 line
drivers/scsi/ufs/ufshcd-crypto.c +1 -0
+		cconfig16 |= ((ctx->cap_idx <<

CRYPTO_CCONFIG16_CAP_IDX_SHIFT) &

Quoted 19 lines
drivers/scsi/ufs/ufshcd-crypto.c +10 -0
+				CRYPTO_CCONFIG16_CAP_IDX_MASK);
 
+		spin_lock(&cctx->crypto_lock);
 
+		for (i = 0; i < key_size; i += 4) {
 
+			val = (ctx->key[i] | (ctx->key[i + 1] << 8) |
 
+				(ctx->key[i + 2] << 16) |
 
+				(ctx->key[i + 3] << 24));
 
+			ufshcd_writel(ctx->hba, val, crypto_config_addr + i);
 
+		}
 
+		ufshcd_writel(ctx->hba, cpu_to_le32(cconfig16),
 
+				crypto_config_addr + (4 * 16));

drivers/scsi/ufs/ufshcd-crypto.c line 162

Quoted 10 lines
drivers/scsi/ufs/ufshcd-crypto.c +5 -0
 
+		spin_unlock(&cctx->crypto_lock);
 
+		/* Make sure keys are programmed */
 
+		mb();
 
+	}
 
+}

First of all, thanks for working on this. A lot of Android device vendors would

like to have upstream support for inline encryption. However, are you aware of

previous (unsuccessful) patchsets by other people working on this? Have you

addressed the concerns and improved on their work, or is this just yet another

new effort starting from scratch?

Quoted 6 lines
Quoted 6 lines

AnilKumar Chimata <[email protected]> (Qualcomm) in October 2018:

https://urldefense.proofpoint.com/v2/url?u=https-

3A__patchwork.kernel.org_cover_10645739_&d=DwIBAg&c=aUq983L2pue2FqKF

Quoted 3 lines
Quoted 3 lines

oP6PGHMJQyoJ7kl3s3GZ-_haXqY&r=GTefrem3hiBCnsjCOqAuapQHRN8- rKC1FRbk0it-

LDs&m=L9VLjsZ31dZ4TP4LdveuvcjPFzdZWGlZaZnzqGZH3zc&s=ZzYaAaVic5TB4RUS

Quoted 8 lines
Quoted 8 lines

cR5kzcM_8gvLYdlNAzuY80_ASzI&e=

Ladvine D Almeida <[email protected]> in May 2018:

Quoted 6 lines
Quoted 6 lines
	https://urldefense.proofpoint.com/v2/url?u=http-3A__lists-

2Darchives.com_linux-2Dkernel_29135393-2Dscsi-2Dufs-2Dufs-2Dhost- 2Dcontroller-2Dcrypto- 2Dchanges.html&d=DwIBAg&c=aUq983L2pue2FqKFoP6PGHMJQyoJ7kl3s3GZ- _haXqY&r=GTefrem3hiBCnsjCOqAuapQHRN8-rKC1FRbk0it-

LDs&m=L9VLjsZ31dZ4TP4LdveuvcjPFzdZWGlZaZnzqGZH3zc&s=3pxSBZrt_DpDSz-

Quoted 14 lines
Quoted 14 lines

ZXrM7_bj0QXmRzcbasPl_wB259Us&e=

Satya Tangirala <[email protected]> is also working on it but I don't believe

he's sent out a patchset yet.

It would be nice to coordinate to get a proper solution upstream that works for

Quoted 7 lines
Quoted 2 lines

everyone, rather than having everyone try independently and fail repeatedly :-)

I had look at Ladvine's submission and think the approach of using Linux crypto API and adding algorithm which is supposed to work inline (and with UFS devices only) in global pool of algorithms (which is supposed to be generic) makes it risky, if selected/used for other type of device not supporting inline encryption. Also apparently it is not possible

to support multiple UFS controllers in the system with that approach.

Quoted 2 lines

There was suggestion from Milan to use separate device mapper which seems cleaner way of enabling inline encryption. Hence new device mapper is

used.

Quoted 1 line

I think this is better idea to coordinate and come up with a generic solution.

Suggest to take a look into the article
https://urldefense.proofpoint.com/v2/url?u=https-
3A__lwn.net_Articles_717754&d=DwIFAg&c=aUq983L2pue2FqKFoP6PGHMJQyoJ 7kl3s3GZ-_haXqY&r=GTefrem3hiBCnsjCOqAuapQHRN8-rKC1FRbk0it- LDs&m=rOQpiy7- jae72qATLWxXtCnoEXCjZLklWrHo5NtcUqo&s=xVHqE7JfiKf0rCQozZrYr6eeFn5D6U OCvXJMP-mjYX8&e=
My real concern is how to achieve it without any modifications to the bio.(because key slot information has to finally reach the target block device)

Quoted 14 lines
Quoted 10 lines

Also, note that ECB mode is not appropriate for disk encryption. So this patch

(and the hardware you tested it on, if that's all it supports) is effectively

useless as-is. You need to support XTS mode.

For now only AES-ECB is supported, we are working on adding other modes.

Quoted 3 lines
Quoted 9 lines
Quoted 5 lines

Thanks!

- Eric

Regards,
Parshuram Thombare

Thanks,
Ladvine