Skip to content

Commit 93f1c15

Browse files
ebiggersUlf Hansson
authored andcommitted
mmc: core: Add basic support for inline encryption
In preparation for adding CQHCI crypto engine (inline encryption) support, add the code required to make mmc_core and mmc_block aware of inline encryption. Specifically: - Add a capability flag MMC_CAP2_CRYPTO to struct mmc_host. Drivers will set this if the host and driver support inline encryption. - Embed a blk_keyslot_manager in struct mmc_host. Drivers will initialize this (as a device-managed resource) if the host and driver support inline encryption. mmc_block registers this keyslot manager with the request_queue of any MMC card attached to the host. - Make mmc_block copy the crypto keyslot and crypto data unit number from struct request to struct mmc_request, so that drivers will have access to them. - If the MMC host is reset, reprogram all the keyslots to ensure that the software state stays in sync with the hardware state. Co-developed-by: Satya Tangirala <satyat@google.com> Signed-off-by: Satya Tangirala <satyat@google.com> Acked-by: Adrian Hunter <adrian.hunter@intel.com> Reviewed-by: Satya Tangirala <satyat@google.com> Reviewed-and-tested-by: Peng Zhou <peng.zhou@mediatek.com> Signed-off-by: Eric Biggers <ebiggers@google.com> Link: https://lore.kernel.org/r/20210126001456.382989-2-ebiggers@kernel.org Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
1 parent d76d9d7 commit 93f1c15

10 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎drivers/mmc/core/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,11 @@ config MMC_TEST
8181
This driver is only of interest to those developing or
8282
testing a host driver. Most people should say N here.
8383

84+
config MMC_CRYPTO
85+
bool "MMC Crypto Engine Support"
86+
depends on BLK_INLINE_ENCRYPTION
87+
help
88+
Enable Crypto Engine Support in MMC.
89+
Enabling this makes it possible for the kernel to use the crypto
90+
capabilities of the MMC device (if present) to perform crypto
91+
operations on data being transferred to/from the device.

‎drivers/mmc/core/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ obj-$(CONFIG_MMC_BLOCK) += mmc_block.o
1818
mmc_block-objs := block.o queue.o
1919
obj-$(CONFIG_MMC_TEST) += mmc_test.o
2020
obj-$(CONFIG_SDIO_UART) += sdio_uart.o
21+
mmc_core-$(CONFIG_MMC_CRYPTO) += crypto.o

‎drivers/mmc/core/block.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "block.h"
5252
#include "core.h"
5353
#include "card.h"
54+
#include "crypto.h"
5455
#include "host.h"
5556
#include "bus.h"
5657
#include "mmc_ops.h"
@@ -1247,6 +1248,8 @@ static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
12471248

12481249
memset(brq, 0, sizeof(struct mmc_blk_request));
12491250

1251+
mmc_crypto_prepare_req(mqrq);
1252+
12501253
brq->mrq.data = &brq->data;
12511254
brq->mrq.tag = req->tag;
12521255

‎drivers/mmc/core/core.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#include "core.h"
3939
#include "card.h"
40+
#include "crypto.h"
4041
#include "bus.h"
4142
#include "host.h"
4243
#include "sdio_bus.h"
@@ -992,6 +993,8 @@ void mmc_set_initial_state(struct mmc_host *host)
992993
host->ops->hs400_enhanced_strobe(host, &host->ios);
993994

994995
mmc_set_ios(host);
996+
997+
mmc_crypto_set_initial_state(host);
995998
}
996999

9971000
/**

‎drivers/mmc/core/crypto.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* MMC crypto engine (inline encryption) support
4+
*
5+
* Copyright 2020 Google LLC
6+
*/
7+
8+
#include <linux/blk-crypto.h>
9+
#include <linux/mmc/host.h>
10+
11+
#include "core.h"
12+
#include "crypto.h"
13+
#include "queue.h"
14+
15+
void mmc_crypto_set_initial_state(struct mmc_host *host)
16+
{
17+
/* Reset might clear all keys, so reprogram all the keys. */
18+
if (host->caps2 & MMC_CAP2_CRYPTO)
19+
blk_ksm_reprogram_all_keys(&host->ksm);
20+
}
21+
22+
void mmc_crypto_setup_queue(struct request_queue *q, struct mmc_host *host)
23+
{
24+
if (host->caps2 & MMC_CAP2_CRYPTO)
25+
blk_ksm_register(&host->ksm, q);
26+
}
27+
EXPORT_SYMBOL_GPL(mmc_crypto_setup_queue);
28+
29+
void mmc_crypto_prepare_req(struct mmc_queue_req *mqrq)
30+
{
31+
struct request *req = mmc_queue_req_to_req(mqrq);
32+
struct mmc_request *mrq = &mqrq->brq.mrq;
33+
34+
if (!req->crypt_keyslot)
35+
return;
36+
37+
mrq->crypto_enabled = true;
38+
mrq->crypto_key_slot = blk_ksm_get_slot_idx(req->crypt_keyslot);
39+
40+
/*
41+
* For now we assume that all MMC drivers set max_dun_bytes_supported=4,
42+
* which is the limit for CQHCI crypto. So all DUNs should be 32-bit.
43+
*/
44+
WARN_ON_ONCE(req->crypt_ctx->bc_dun[0] > U32_MAX);
45+
46+
mrq->data_unit_num = req->crypt_ctx->bc_dun[0];
47+
}
48+
EXPORT_SYMBOL_GPL(mmc_crypto_prepare_req);

‎drivers/mmc/core/crypto.h‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* MMC crypto engine (inline encryption) support
4+
*
5+
* Copyright 2020 Google LLC
6+
*/
7+
8+
#ifndef _MMC_CORE_CRYPTO_H
9+
#define _MMC_CORE_CRYPTO_H
10+
11+
struct mmc_host;
12+
struct mmc_queue_req;
13+
struct request_queue;
14+
15+
#ifdef CONFIG_MMC_CRYPTO
16+
17+
void mmc_crypto_set_initial_state(struct mmc_host *host);
18+
19+
void mmc_crypto_setup_queue(struct request_queue *q, struct mmc_host *host);
20+
21+
void mmc_crypto_prepare_req(struct mmc_queue_req *mqrq);
22+
23+
#else /* CONFIG_MMC_CRYPTO */
24+
25+
static inline void mmc_crypto_set_initial_state(struct mmc_host *host)
26+
{
27+
}
28+
29+
static inline void mmc_crypto_setup_queue(struct request_queue *q,
30+
struct mmc_host *host)
31+
{
32+
}
33+
34+
static inline void mmc_crypto_prepare_req(struct mmc_queue_req *mqrq)
35+
{
36+
}
37+
38+
#endif /* !CONFIG_MMC_CRYPTO */
39+
40+
#endif /* _MMC_CORE_CRYPTO_H */

‎drivers/mmc/core/host.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/mmc/slot-gpio.h>
2626

2727
#include "core.h"
28+
#include "crypto.h"
2829
#include "host.h"
2930
#include "slot-gpio.h"
3031
#include "pwrseq.h"

‎drivers/mmc/core/queue.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "block.h"
2020
#include "core.h"
2121
#include "card.h"
22+
#include "crypto.h"
2223
#include "host.h"
2324

2425
#define MMC_DMA_MAP_MERGE_SEGMENTS 512
@@ -407,6 +408,8 @@ static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
407408
mutex_init(&mq->complete_lock);
408409

409410
init_waitqueue_head(&mq->wait);
411+
412+
mmc_crypto_setup_queue(mq->queue, host);
410413
}
411414

412415
static inline bool mmc_merge_capable(struct mmc_host *host)

‎include/linux/mmc/core.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ struct mmc_request {
162162
bool cap_cmd_during_tfr;
163163

164164
int tag;
165+
166+
#ifdef CONFIG_MMC_CRYPTO
167+
bool crypto_enabled;
168+
int crypto_key_slot;
169+
u32 data_unit_num;
170+
#endif
165171
};
166172

167173
struct mmc_card;

‎include/linux/mmc/host.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/mmc/card.h>
1616
#include <linux/mmc/pm.h>
1717
#include <linux/dma-direction.h>
18+
#include <linux/keyslot-manager.h>
1819

1920
struct mmc_ios {
2021
unsigned int clock; /* clock rate */
@@ -395,6 +396,11 @@ struct mmc_host {
395396
#define MMC_CAP2_CQE_DCMD (1 << 24) /* CQE can issue a direct command */
396397
#define MMC_CAP2_AVOID_3_3V (1 << 25) /* Host must negotiate down from 3.3V */
397398
#define MMC_CAP2_MERGE_CAPABLE (1 << 26) /* Host can merge a segment over the segment size */
399+
#ifdef CONFIG_MMC_CRYPTO
400+
#define MMC_CAP2_CRYPTO (1 << 27) /* Host supports inline encryption */
401+
#else
402+
#define MMC_CAP2_CRYPTO 0
403+
#endif
398404

399405
int fixed_drv_type; /* fixed driver type for non-removable media */
400406

@@ -489,6 +495,11 @@ struct mmc_host {
489495
bool cqe_enabled;
490496
bool cqe_on;
491497

498+
/* Inline encryption support */
499+
#ifdef CONFIG_MMC_CRYPTO
500+
struct blk_keyslot_manager ksm;
501+
#endif
502+
492503
/* Host Software Queue support */
493504
bool hsq_enabled;
494505

0 commit comments

Comments
 (0)