xref: /linux/drivers/mmc/core/crypto.c (revision 86c639ce08266ed521974038f0592739fec1c11a)
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_ctx)
35 		return;
36 
37 	mrq->crypto_ctx = req->crypt_ctx;
38 	if (req->crypt_keyslot)
39 		mrq->crypto_key_slot = blk_ksm_get_slot_idx(req->crypt_keyslot);
40 }
41 EXPORT_SYMBOL_GPL(mmc_crypto_prepare_req);
42