15 #include <linux/device.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
27 #include <linux/bitops.h>
30 #include <crypto/sha.h>
35 #include <mach/hardware.h>
39 #define DEV_DBG_NAME "hashX hashX:"
49 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
50 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
51 0xaf, 0xd8, 0x07, 0x09
55 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
56 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
57 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
58 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
63 0xfb, 0xdb, 0x1d, 0x1b, 0x18, 0xaa, 0x6c, 0x08,
64 0x32, 0x4b, 0x7d, 0x64, 0xb7, 0x1f, 0xb7, 0x63,
65 0x70, 0x69, 0x0e, 0x1d
70 0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec,
71 0x77, 0x2f, 0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5,
72 0xff, 0x16, 0x97, 0xc4, 0x93, 0x71, 0x56, 0x53,
73 0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 0xc5, 0xad
113 spin_unlock(&device_data->
ctx_lock);
130 device_data->
dma.chan_mem2hash =
133 device_data->
dma.cfg_mem2hash);
135 init_completion(&device_data->
dma.complete);
138 static void hash_dma_callback(
void *
data)
160 channel = ctx->
device->dma.chan_mem2hash;
166 if (!ctx->
device->dma.sg_len) {
168 "[%s]: Could not map the sg list (TO_DEVICE)",
174 "(TO_DEVICE)", __func__);
175 desc = channel->
device->device_prep_slave_sg(channel,
180 "[%s]: device_prep_slave_sg() failed!", __func__);
188 dma_async_issue_pending(channel);
193 static void hash_dma_done(
struct hash_ctx *ctx)
197 chan = ctx->
device->dma.chan_mem2hash;
204 static int hash_dma_write(
struct hash_ctx *ctx,
225 static int get_empty_message_digest(
227 u8 *zero_hash,
u32 *zero_hash_size,
bool *zero_digest)
231 *zero_digest =
false;
239 memcpy(zero_hash, &zero_message_hash_sha1[0],
245 memcpy(zero_hash, &zero_message_hash_sha256[0],
251 "Incorrect algorithm!"
259 memcpy(zero_hash, &zero_message_hmac_sha1[0],
264 memcpy(zero_hash, &zero_message_hmac_sha256[0],
270 "Incorrect algorithm!"
276 dev_dbg(device_data->
dev,
"[%s] Continue hash "
277 "calculation, since hmac key avalable",
294 static int hash_disable_power(
296 bool save_device_state)
305 if (save_device_state) {
307 &device_data->
state);
314 dev_err(dev,
"[%s] regulator_disable() failed!", __func__);
332 static int hash_enable_power(
334 bool restore_device_state)
343 dev_err(dev,
"[%s]: regulator_enable() failed!",
349 dev_err(dev,
"[%s]: clk_enable() failed!",
359 if (restore_device_state) {
362 &device_data->
state);
380 static int hash_get_device_data(
struct hash_ctx *ctx,
396 while (device_node) {
399 spin_lock(&local_device_data->
ctx_lock);
405 ctx->
device = local_device_data;
406 spin_unlock(&local_device_data->
ctx_lock);
409 spin_unlock(&local_device_data->
ctx_lock);
425 *device_data = local_device_data;
442 const u8 *
key,
unsigned int keylen)
449 while (keylen >= 4) {
450 u32 *key_word = (
u32 *)key;
461 word |= (key[keylen - 1] << (8 * (keylen - 1)));
492 dev_err(device_data->
dev,
"[%s] hash_setconfiguration() "
493 "failed!", __func__);
500 hash_hw_write_key(device_data, ctx->
key, ctx->
keylen);
516 bool aligned_data =
true;
518 while (size > 0 && sg) {
526 aligned_data =
false;
532 *aligned = aligned_data;
548 static bool hash_dma_valid_data(
struct scatterlist *sg,
int datasize)
553 if (hash_get_nents(sg, datasize, &aligned) < 1)
568 struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
581 "to CPU mode for data size < %d",
585 hash_dma_valid_data(req->
src,
591 " CPU mode for datalength < %d"
592 " or non-aligned data, except "
593 "in last nent", __func__,
609 static void hash_processblock(
647 while (index_bytes >= 4) {
661 dev_dbg(device_data->
dev,
"[%s] DIN=0x%08x NBLW=%d", __func__,
666 dev_dbg(device_data->
dev,
"[%s] after dcal -> DIN=0x%08x NBLW=%d",
685 ctx->
state.length.low_word += incr;
688 if (ctx->
state.length.low_word < incr)
689 ctx->
state.length.high_word++;
726 dev_err(device_data->
dev,
"[%s] Incorrect algorithm.",
743 dev_dbg(device_data->
dev,
"[%s] LKEY set", __func__);
747 dev_dbg(device_data->
dev,
"[%s] LKEY cleared",
754 dev_err(device_data->
dev,
"[%s] HASH_INVALID_PARAMETER!",
797 for (count = 0; count < msg_length; count++) {
798 buffer[*index +
count] =
799 *(data_buffer +
count);
801 *index += msg_length;
807 &device_data->
state);
809 device_data->
state.buffer,
813 "hash_resume_state()"
814 " failed!", __func__);
818 ret = init_hash_hw(device_data, ctx);
822 " failed!", __func__);
834 if ((0 == (((
u32)data_buffer) % 4))
836 hash_processblock(device_data,
840 for (count = 0; count <
844 buffer[*index +
count] =
845 *(data_buffer +
count);
847 hash_processblock(device_data,
858 &device_data->
state);
861 req_ctx->
state.buffer,
866 " failed!", __func__);
870 }
while (msg_length != 0);
884 struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
888 int bytes_written = 0;
890 ret = hash_get_device_data(ctx, &device_data);
894 dev_dbg(device_data->
dev,
"[%s] (ctx=0x%x)!", __func__, (
u32) ctx);
900 dev_err(device_data->
dev,
"[%s] hash_resume_state() "
901 "failed!", __func__);
911 "hash_setconfiguration() failed!",
930 hash_hw_write_key(device_data, ctx->
key, ctx->
keylen);
939 if (!ctx->
device->dma.nents) {
941 "ctx->device->dma.nents = 0", __func__);
945 bytes_written = hash_dma_write(ctx, req->
src, req->
nbytes);
946 if (bytes_written != req->
nbytes) {
948 "hash_dma_write() failed!", __func__);
959 unsigned int keylen = ctx->
keylen;
962 dev_dbg(device_data->
dev,
"[%s] keylen: %d", __func__,
964 hash_hw_write_key(device_data, key, keylen);
971 release_hash_device(device_data);
989 struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
994 ret = hash_get_device_data(ctx, &device_data);
998 dev_dbg(device_data->
dev,
"[%s] (ctx=0x%x)!", __func__, (
u32) ctx);
1004 dev_err(device_data->
dev,
"[%s] hash_resume_state() "
1005 "failed!", __func__);
1008 }
else if (req->
nbytes == 0 && ctx->keylen == 0) {
1010 u32 zero_hash_size = 0;
1011 bool zero_digest =
false;
1016 ret = get_empty_message_digest(device_data, &zero_hash[0],
1017 &zero_hash_size, &zero_digest);
1018 if (!ret &&
likely(zero_hash_size == ctx->digestsize) &&
1022 }
else if (!ret && !zero_digest) {
1023 dev_dbg(device_data->
dev,
"[%s] HMAC zero msg with "
1024 "key, continue...", __func__);
1026 dev_err(device_data->
dev,
"[%s] ret=%d, or wrong "
1027 "digest size? %s", __func__, ret,
1028 (zero_hash_size == ctx->digestsize) ?
1033 }
else if (req->
nbytes == 0 && ctx->keylen > 0) {
1034 dev_err(device_data->
dev,
"[%s] Empty message with "
1035 "keylength > 0, NOT supported.", __func__);
1040 ret = init_hash_hw(device_data, ctx);
1042 dev_err(device_data->
dev,
"[%s] init_hash_hw() "
1043 "failed!", __func__);
1048 if (req_ctx->
state.index) {
1049 hash_messagepad(device_data, req_ctx->
state.buffer,
1050 req_ctx->
state.index);
1058 unsigned int keylen = ctx->keylen;
1061 dev_dbg(device_data->
dev,
"[%s] keylen: %d", __func__,
1063 hash_hw_write_key(device_data, key, keylen);
1070 release_hash_device(device_data);
1094 struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1100 if (msg_length == 0)
1103 index = req_ctx->
state.index;
1104 buffer = (
u8 *)req_ctx->
state.buffer;
1108 if (msg_length > (req_ctx->
state.length.low_word + msg_length) &&
1110 req_ctx->
state.length.high_word) {
1116 ret = hash_get_device_data(ctx, &device_data);
1121 while (0 != msg_length) {
1122 data_buffer = walk.
data;
1124 data_buffer, buffer, &index);
1127 dev_err(device_data->
dev,
"[%s] hash_internal_hw_"
1128 "update() failed!", __func__);
1136 dev_dbg(device_data->
dev,
"[%s] indata length=%d, bin=%d))",
1137 __func__, req_ctx->
state.index,
1138 req_ctx->
state.bit_index);
1141 release_hash_device(device_data);
1158 if (
NULL == device_state) {
1159 dev_err(device_data->
dev,
"[%s] HASH_INVALID_PARAMETER!",
1167 dev_err(device_data->
dev,
"[%s] HASH_INVALID_PARAMETER!",
1179 temp_cr = device_state->
temp_cr;
1191 writel_relaxed(device_state->
csr[count],
1192 &device_data->
base->csrx[count]);
1195 writel_relaxed(device_state->
csfull, &device_data->
base->csfull);
1196 writel_relaxed(device_state->
csdatain, &device_data->
base->csdatain);
1198 writel_relaxed(device_state->
str_reg, &device_data->
base->str);
1199 writel_relaxed(temp_cr, &device_data->
base->cr);
1216 if (
NULL == device_state) {
1217 dev_err(device_data->
dev,
"[%s] HASH_INVALID_PARAMETER!",
1251 device_state->
temp_cr = temp_cr;
1276 dev_err(device_data->
dev,
"[%s] HASH_UNSUPPORTED_HW!",
1294 dev_err(device_data->
dev,
"[%s] Incorrect algorithm %d",
1295 __func__, algorithm);
1304 dev_dbg(device_data->
dev,
"[%s] digest array:(0x%x)",
1305 __func__, (
u32) digest);
1308 for (count = 0; count < loop_ctr; count++) {
1310 digest[count * 4] = (
u8) ((temp_hx_val >> 24) & 0xFF);
1311 digest[count * 4 + 1] = (
u8) ((temp_hx_val >> 16) & 0xFF);
1312 digest[count * 4 + 2] = (
u8) ((temp_hx_val >> 8) & 0xFF);
1313 digest[count * 4 + 3] = (
u8) ((temp_hx_val >> 0) & 0xFF);
1350 ret = hash_dma_final(req);
1363 const u8 *key,
unsigned int keylen,
int alg)
1366 struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1374 "for %d\n", __func__, alg);
1387 struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1400 struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1414 ret1 = ahash_sha1_init(req);
1418 ret1 = ahash_update(req);
1419 ret2 = ahash_final(req);
1422 return ret1 ? ret1 : ret2;
1429 ret1 = ahash_sha256_init(req);
1433 ret1 = ahash_update(req);
1434 ret2 = ahash_final(req);
1437 return ret1 ? ret1 : ret2;
1443 struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1456 struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1470 ret1 = hmac_sha1_init(req);
1474 ret1 = ahash_update(req);
1475 ret2 = ahash_final(req);
1478 return ret1 ? ret1 : ret2;
1485 ret1 = hmac_sha256_init(req);
1489 ret1 = ahash_update(req);
1490 ret2 = ahash_final(req);
1493 return ret1 ? ret1 : ret2;
1497 const u8 *key,
unsigned int keylen)
1502 static int hmac_sha256_setkey(
struct crypto_ahash *tfm,
1503 const u8 *key,
unsigned int keylen)
1513 static int hash_cra_init(
struct crypto_tfm *tfm)
1515 struct hash_ctx *ctx = crypto_tfm_ctx(tfm);
1523 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1527 ctx->
config.algorithm = hash_alg->
conf.algorithm;
1528 ctx->
config.oper_mode = hash_alg->
conf.oper_mode;
1541 .update = ahash_update,
1542 .final = ahash_final,
1543 .digest = ahash_sha1_digest,
1545 .halg.statesize =
sizeof(
struct hash_ctx),
1548 .cra_driver_name =
"sha1-ux500",
1552 .cra_ctxsize =
sizeof(
struct hash_ctx),
1553 .cra_init = hash_cra_init,
1563 .update = ahash_update,
1564 .final = ahash_final,
1565 .digest = ahash_sha256_digest,
1567 .halg.statesize =
sizeof(
struct hash_ctx),
1569 .cra_name =
"sha256",
1570 .cra_driver_name =
"sha256-ux500",
1574 .cra_ctxsize =
sizeof(
struct hash_ctx),
1576 .cra_init = hash_cra_init,
1587 .update = ahash_update,
1588 .final = ahash_final,
1589 .digest = hmac_sha1_digest,
1590 .setkey = hmac_sha1_setkey,
1592 .halg.statesize =
sizeof(
struct hash_ctx),
1594 .cra_name =
"hmac(sha1)",
1595 .cra_driver_name =
"hmac-sha1-ux500",
1599 .cra_ctxsize =
sizeof(
struct hash_ctx),
1601 .cra_init = hash_cra_init,
1611 .update = ahash_update,
1612 .final = ahash_final,
1613 .digest = hmac_sha256_digest,
1614 .setkey = hmac_sha256_setkey,
1616 .halg.statesize =
sizeof(
struct hash_ctx),
1618 .cra_name =
"hmac(sha256)",
1619 .cra_driver_name =
"hmac-sha256-ux500",
1623 .cra_ctxsize =
sizeof(
struct hash_ctx),
1625 .cra_init = hash_cra_init,
1641 for (i = 0; i <
ARRAY_SIZE(hash_algs); i++) {
1645 dev_err(device_data->
dev,
"[%s] alg registration failed",
1646 hash_algs[i].
hash.halg.base.cra_driver_name);
1652 for (i = 0; i <
count; i++)
1660 static void ahash_algs_unregister_all(
struct hash_device_data *device_data)
1681 dev_dbg(dev,
"[%s] kzalloc() failed!", __func__);
1691 dev_dbg(dev,
"[%s] platform_get_resource() failed!", __func__);
1698 dev_dbg(dev,
"[%s] request_mem_region() failed!", __func__);
1704 if (!device_data->
base) {
1705 dev_err(dev,
"[%s] ioremap() failed!",
1716 dev_err(dev,
"[%s] regulator_get() failed!", __func__);
1724 if (IS_ERR(device_data->
clk)) {
1725 dev_err(dev,
"[%s] clk_get() failed!", __func__);
1726 ret = PTR_ERR(device_data->
clk);
1731 ret = hash_enable_power(device_data,
false);
1733 dev_err(dev,
"[%s]: hash_enable_power() failed!", __func__);
1739 dev_err(dev,
"[%s] hash_check_hw() failed!", __func__);
1744 hash_dma_setup_channel(device_data, dev);
1746 platform_set_drvdata(pdev, device_data);
1753 ret = ahash_algs_register_all(device_data);
1755 dev_err(dev,
"[%s] ahash_algs_register_all() "
1756 "failed!", __func__);
1760 dev_info(dev,
"[%s] successfully probed\n", __func__);
1764 hash_disable_power(device_data,
false);
1794 device_data = platform_get_drvdata(pdev);
1796 dev_err(dev,
"[%s]: platform_get_drvdata() failed!",
1810 spin_unlock(&device_data->
ctx_lock);
1816 spin_unlock(&device_data->
ctx_lock);
1824 ahash_algs_unregister_all(device_data);
1826 if (hash_disable_power(device_data,
false))
1827 dev_err(dev,
"[%s]: hash_disable_power() failed",
1853 device_data = platform_get_drvdata(pdev);
1855 dev_err(&pdev->
dev,
"[%s] platform_get_drvdata() failed!",
1865 dev_dbg(&pdev->
dev,
"[%s]: Cryp still in use!"
1866 "Shutting down anyway...", __func__);
1874 spin_unlock(&device_data->
ctx_lock);
1882 ahash_algs_unregister_all(device_data);
1890 if (hash_disable_power(device_data,
false))
1891 dev_err(&pdev->
dev,
"[%s] hash_disable_power() failed",
1899 static int ux500_hash_suspend(
struct device *dev)
1907 dev_err(dev,
"[%s] platform_get_drvdata() failed!", __func__);
1914 spin_unlock(&device_data->
ctx_lock);
1918 dev_dbg(dev,
"[%s]: down_interruptible() failed",
1920 ret = hash_disable_power(device_data,
false);
1923 ret = hash_disable_power(device_data,
true);
1926 dev_err(dev,
"[%s]: hash_disable_power()", __func__);
1935 static int ux500_hash_resume(
struct device *dev)
1943 dev_err(dev,
"[%s] platform_get_drvdata() failed!", __func__);
1950 spin_unlock(&device_data->
ctx_lock);
1955 ret = hash_enable_power(device_data,
true);
1958 dev_err(dev,
"[%s]: hash_enable_power() failed!", __func__);
1966 .probe = ux500_hash_probe,
1967 .remove = ux500_hash_remove,
1968 .shutdown = ux500_hash_shutdown,
1972 .pm = &ux500_hash_pm,
1979 static int __init ux500_hash_mod_init(
void)
1991 static void __exit ux500_hash_mod_fini(
void)