summaryrefstree
path: root/kernel/drivers/mmc/host/dw_mmc-exynos-fmp.c
blob: dd5d7a54e1eb5e003eed812026351e146ed56fcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 * Copyright (C) 2016 Samsung Electronics Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include <linux/semaphore.h>
#include <linux/blkdev.h>
#include <linux/mmc/core.h>
#include <linux/mmc/host.h>
#include <linux/mmc/dw_mmc.h>
#include <linux/mmc/mmc.h>

#include "dw_mmc.h"
#include "dw_mmc-exynos.h"
#include "../card/queue.h"

static int check_data_equal(void *data1, void *data2)
{
	return data1 == data2;
}

static int is_valid_bio_data(struct bio *bio)
{
	if (bio->fmp_ci.private_enc_mode < 0 ||
			bio->fmp_ci.private_enc_mode > EXYNOS_FMP_DISK_ENC)
		return false;

	if (bio->fmp_ci.private_algo_mode < 0 ||
			bio->fmp_ci.private_algo_mode > EXYNOS_FMP_ALGO_MODE_AES_XTS)
		return false;

	return true;
}

static struct bio *is_get_bio(struct mmc_data *data, bool cmdq_enabled)
{
	struct bio *bio = NULL;

	if (!data) {
		pr_err("%s: Invalid MMC data\n", __func__);
		return NULL;
	}

	if (cmdq_enabled) {
#if 0 /* CMDQ is not implemented on the current kernel */
		struct mmc_cmdq_req *cmdq_req;
		struct mmc_request *mrq;

		cmdq_req = container_of(data, struct mmc_cmdq_req, data);
		if (!cmdq_req)
			return NULL;

		mrq = &cmdq_req->mrq;
		if (!mrq || !mrq->req || !mrq->req->bio)
			return NULL;

		bio = mrq->req->bio;
#endif
	} else {
		struct mmc_queue_req *mq_rq;
		struct mmc_blk_request *brq;

		brq = container_of(data, struct mmc_blk_request, data);
		if (!brq)
			return NULL;

		mq_rq = container_of(brq, struct mmc_queue_req, brq);
		if (!mq_rq || !mq_rq->req || !mq_rq->req->bio)
			return NULL;
		bio = mq_rq->req->bio;

		if (!virt_addr_valid(bio))
			return NULL;
	}

	return bio;
}

static int is_mmc_fmp_test_enabled(struct mmc_data *mmc_data,
				struct platform_device *pdev,
				bool cmdq_enabled)
{
	struct bio *bio;
	struct exynos_fmp *fmp = dev_get_drvdata(&pdev->dev);

	if (!fmp)
		return FALSE;

	bio = is_get_bio(mmc_data, cmdq_enabled);
	if (!bio) {
		fmp->test_mode = 0;
		return FALSE;
	}

	if (check_data_equal((void *)bio->bi_private, (void *)fmp->test_bh)
			&& (uint64_t)fmp->test_bh) {
		fmp->test_mode = 1;
		return TRUE;
	}

	fmp->test_mode = 0;
	return FALSE;
}

static int exynos_mmc_fmp_key_size_cfg(struct fmp_crypto_setting *crypto,
					uint32_t key_size)
{
	int ret = 0;
	uint32_t size;

	if (!crypto || !key_size) {
		pr_err("%s: Invalid fmp data or size.\n", __func__);
		ret = -EINVAL;
		goto out;
	}

	size = key_size;
	if (crypto->algo_mode == EXYNOS_FMP_ALGO_MODE_AES_XTS)
		size = size >> 1;

	switch (size) {
	case FMP_KEY_SIZE_16:
		crypto->key_size = EXYNOS_FMP_KEY_SIZE_16;
		break;
	case FMP_KEY_SIZE_32:
		crypto->key_size = EXYNOS_FMP_KEY_SIZE_32;
		break;
	default:
		pr_err("%s: FMP doesn't support key size %d\n", __func__, size);
		ret = -EINVAL;
		goto out;
	}
out:
	return ret;
}

static int exynos_mmc_fmp_iv_cfg(struct fmp_crypto_setting *crypto,
					sector_t sector,
					int sector_offset,
					pgoff_t page_index)
{
	int ret = 0;

	if (!crypto) {
		pr_err("%s: Invalid fmp data\n", __func__);
		ret = -EINVAL;
		goto out;
	}

	crypto->index = page_index;
	crypto->sector = sector + sector_offset;
out:
	return ret;
}

static int exynos_mmc_fmp_key_cfg(struct fmp_crypto_setting *crypto,
				unsigned char *key, unsigned long key_length)
{
	int ret = 0;

	if (!crypto) {
		pr_err("%s: Invalid fmp data\n", __func__);
		ret = -EINVAL;
		goto out;
	}
	memset(crypto->key, 0, FMP_MAX_KEY_SIZE);
	memcpy(crypto->key, key, key_length);
out:
	return ret;
}

static int exynos_mmc_fmp_disk_cfg(struct mmc_data *mmc_data,
				struct fmp_crypto_setting *crypto,
				int sector_offset, bool cmdq_enabled)
{
	int ret = 0;
	struct bio *bio;

	if (!crypto) {
		pr_err("%s: Invalid fmp data\n", __func__);
		ret = -EINVAL;
		goto err;
	}

	memset(crypto, 0, sizeof(struct fmp_crypto_setting));

	crypto->enc_mode = EXYNOS_FMP_DISK_ENC;

	bio = is_get_bio(mmc_data, cmdq_enabled);
	if (!bio)
		goto bypass_out;

	if ((bio->fmp_ci.private_algo_mode == EXYNOS_FMP_BYPASS_MODE) ||
			/* direct IO case */
			(bio->fmp_ci.private_enc_mode == EXYNOS_FMP_FILE_ENC))
		goto bypass_out;

	if (!is_valid_bio_data(bio))
		goto bypass_out;

	if (!bio->fmp_ci.key) {
		pr_err("%s: Invalid disk key\n", __func__);
		ret = -EINVAL;
		goto err;
	}

	crypto->algo_mode = bio->fmp_ci.private_algo_mode;
	ret = exynos_mmc_fmp_key_size_cfg(crypto, bio->fmp_ci.key_length);
	if (ret)
		goto bypass_out;

	ret = exynos_mmc_fmp_iv_cfg(crypto, bio->bi_iter.bi_sector,
					sector_offset, 0);
	if (ret) {
		pr_err("%s: Fail to configure fmp iv. ret(%d)\n",
				__func__, ret);
		ret = -EINVAL;
		goto err;
	}
err:
	return ret;
bypass_out:
	crypto->algo_mode = EXYNOS_FMP_BYPASS_MODE;
	ret = 0;

	return ret;
}

static int exynos_mmc_fmp_direct_io_cfg(struct mmc_data *mmc_data,
				struct fmp_crypto_setting *crypto,
				int sector_offset, bool cmdq_enabled)
{
	int ret = 0;
	struct bio *bio;

	if (!crypto) {
		pr_err("%s: Invalid fmp data\n", __func__);
		ret = -EINVAL;
		goto err;
	}

	memset(crypto, 0, sizeof(struct fmp_crypto_setting));

	crypto->enc_mode = EXYNOS_FMP_FILE_ENC;

	bio = is_get_bio(mmc_data, cmdq_enabled);
	if (!bio || (bio->fmp_ci.private_algo_mode == EXYNOS_FMP_BYPASS_MODE))
		goto bypass_out;

	if (!is_valid_bio_data(bio))
		goto bypass_out;

	crypto->algo_mode = bio->fmp_ci.private_algo_mode;
	ret = exynos_mmc_fmp_key_size_cfg(crypto, bio->fmp_ci.key_length);
	if (ret)
		goto bypass_out;

	ret = exynos_mmc_fmp_iv_cfg(crypto, bio->bi_iter.bi_sector,
					sector_offset, 0);
	if (ret) {
		pr_err("%s: Fail to configure fmp iv. ret(%d)\n",
				__func__, ret);
		ret = -EINVAL;
		goto err;
	}

	ret = exynos_mmc_fmp_key_cfg(crypto, bio->fmp_ci.key,
					bio->fmp_ci.key_length);
	if (ret) {
		pr_err("%s: Fail to configure fmp key. ret(%d)\n",
				__func__, ret);
		ret = -EINVAL;
		goto err;
	}
err:
	return ret;
bypass_out:
	crypto->algo_mode = EXYNOS_FMP_BYPASS_MODE;
	ret = 0;
	return ret;
}

static int exynos_mmc_fmp_file_cfg(struct mmc_data *mmc_data,
				struct page *page,
				struct fmp_crypto_setting *crypto,
				int sector_offset, bool cmdq_enabled)
{
	int ret = 0;
	struct bio *bio;
	pgoff_t page_index;
	struct _fmp_ci *ci;

	if (!crypto) {
		pr_err("%s: Invalid fmp data\n", __func__);
		ret = -EINVAL;
		goto err;
	}

	memset(crypto, 0, sizeof(struct fmp_crypto_setting));

	crypto->enc_mode = EXYNOS_FMP_FILE_ENC;

	if (!page || !page->mapping || PageAnon(page))
		goto bypass_out;

	bio = is_get_bio(mmc_data, cmdq_enabled);
	if (!bio || !virt_addr_valid(bio))
		goto bypass_out;

	ci = &page->mapping->fmp_ci;
	if (ci->private_algo_mode == EXYNOS_FMP_BYPASS_MODE)
		goto bypass_out;

	crypto->algo_mode = ci->private_algo_mode;
	ret = exynos_mmc_fmp_key_size_cfg(crypto, ci->key_length);
	if (ret)
		goto bypass_out;

	ret = exynos_mmc_fmp_iv_cfg(crypto, bio->bi_iter.bi_sector,
					sector_offset, page_index);
	if (ret) {
		pr_err("%s: Fail to configure fmp iv. ret(%d)\n",
				__func__, ret);
		ret = -EINVAL;
		goto err;
	}

	ret = exynos_mmc_fmp_key_cfg(crypto, ci->key, ci->key_length);
	if (ret) {
		pr_err("%s: Fail to configure fmp key. ret(%d)\n",
				__func__, ret);
		ret = -EINVAL;
		goto err;
	}
err:
	return ret;
bypass_out:
	crypto->algo_mode = EXYNOS_FMP_BYPASS_MODE;
	ret = 0;
	return ret;
}

int exynos_mmc_fmp_host_set_device(struct platform_device *host_pdev,
				struct platform_device *pdev,
				struct exynos_fmp_variant_ops *fmp_vops)
{
	struct dw_mci *host;
	struct dw_mci_exynos_priv_data *priv;

	if (!host_pdev || !pdev || !fmp_vops) {
		pr_err("%s: Fail to set device for fmp host\n", __func__);
		return -EINVAL;
	}

	host = dev_get_drvdata(&host_pdev->dev);
	if (!host) {
		pr_err("%s: Invalid Host device\n", __func__);
		return -ENODEV;
	}

	priv = host->priv;
	if (!priv) {
		pr_err("%s: Invalid Host device private data\n", __func__);
		return -ENODEV;
	}

	priv->fmp.pdev = pdev;
	priv->fmp.vops = fmp_vops;

	return 0;
}
EXPORT_SYMBOL(exynos_mmc_fmp_host_set_device);

static inline void exynos_mmc_fmp_bypass(void *desc, bool cmdq_enabled)
{
	if (cmdq_enabled) {
		SET_CMDQ_FAS((struct fmp_table_setting *)desc, 0);
		SET_CMDQ_DAS((struct fmp_table_setting *)desc, 0);
	} else {
		SET_FAS((struct fmp_table_setting *)desc, 0);
		SET_DAS((struct fmp_table_setting *)desc, 0);
	}
	return;
}

int exynos_mmc_fmp_cfg(struct dw_mci *host,
				void *desc,
				struct mmc_data *mmc_data,
				struct page *page,
				int sector_offset,
				bool cmdq_enabled)
{
	int ret;
	struct fmp_data_setting data;
	struct dw_mci_exynos_priv_data *priv;

	if (!host) {
		pr_err("%s: Invalid Host device\n", __func__);
		return -ENODEV;
	}

	priv = host->priv;
	if (!priv || !priv->fmp.pdev) {
		exynos_mmc_fmp_bypass(desc, cmdq_enabled);
		return 0;
	}

	ret = is_mmc_fmp_test_enabled(mmc_data, priv->fmp.pdev, cmdq_enabled);
	if (ret == TRUE)
		goto out_test;

	ret = exynos_mmc_fmp_disk_cfg(mmc_data, &data.disk, sector_offset, cmdq_enabled);
	if (ret) {
		pr_err("%s: Fail to configure FMP Disk Encryption. ret(%d)\n",
				__func__, ret);
		return -EINVAL;
	}

	if (data.disk.algo_mode != EXYNOS_FMP_BYPASS_MODE)
		goto file_cfg;

	ret = exynos_mmc_fmp_direct_io_cfg(mmc_data, &data.file,
			sector_offset, cmdq_enabled);
	if (ret) {
		pr_err("%s: Fail to configure FMP direct IO File Encryption. ret(%d)\n",
				__func__, ret);
		return -EINVAL;
	}

	if (data.file.algo_mode != EXYNOS_FMP_BYPASS_MODE)
		goto out;

file_cfg:
	ret = exynos_mmc_fmp_file_cfg(mmc_data, page, &data.file,
			sector_offset, cmdq_enabled);
	if (ret) {
		pr_err("%s: Fail to configure FMP File Encryption. ret(%d)\n",
				__func__, ret);
		return -EINVAL;
	}

out:
	data.mapping = page->mapping;
out_test:
	data.table = (struct fmp_table_setting *)desc;
	data.cmdq_enabled = cmdq_enabled;
	return priv->fmp.vops->config(priv->fmp.pdev, &data);
}
EXPORT_SYMBOL(exynos_mmc_fmp_cfg);

int exynos_mmc_fmp_clear(struct dw_mci *host, void *desc, bool cmdq_enabled)
{
	int ret = 0;
	struct dw_mci_exynos_priv_data *priv;
	struct fmp_data_setting data;

	if (!host) {
		pr_err("%s: Invalid Host device\n", __func__);
		ret = -ENODEV;
		goto err;
	}

	priv = host->priv;
	if (!priv || !priv->fmp.pdev) {
		ret = 0;
		goto err;
	}

	data.table = (struct fmp_table_setting *)desc;
	if (cmdq_enabled) {
		if (!GET_CMDQ_FAS(data.table))
			return ret;
	} else {
		if (!GET_FAS(data.table))
			return ret;
	}

	ret = priv->fmp.vops->clear(priv->fmp.pdev, &data);
	if (ret) {
		pr_err("%s: Fail to clear FMP desc (%d)\n",
			__func__, ret);
		ret = -EINVAL;
		goto err;
	}
err:
	return ret;
}