19 #include <crypto/aes.h>
24 #include <crypto/sha.h>
35 #include <linux/list.h>
36 #include <linux/module.h>
40 #include <linux/rtnetlink.h>
42 #include <linux/sched.h>
43 #include <linux/slab.h>
53 #define CMD0_IRQ_THRESHOLD 1
62 #define PACKET_TIMEOUT 1
65 #define SPACC_CRYPTO_ALG_PRIORITY 10000
67 #define SPACC_CRYPTO_KASUMI_F8_KEY_LEN 16
68 #define SPACC_CRYPTO_IPSEC_CIPHER_PG_SZ 64
69 #define SPACC_CRYPTO_IPSEC_HASH_PG_SZ 64
70 #define SPACC_CRYPTO_IPSEC_MAX_CTXS 32
71 #define SPACC_CRYPTO_IPSEC_FIFO_SZ 32
72 #define SPACC_CRYPTO_L2_CIPHER_PG_SZ 64
73 #define SPACC_CRYPTO_L2_HASH_PG_SZ 64
74 #define SPACC_CRYPTO_L2_MAX_CTXS 128
75 #define SPACC_CRYPTO_L2_FIFO_SZ 128
77 #define MAX_DDT_LEN 16
135 #define SPACC_CRYPTO_ALG_MASK 0x7
205 return is_cipher_ctx ? ctx->
engine->cipher_ctx_base +
206 (indx * ctx->
engine->cipher_pg_sz) :
207 ctx->
engine->hash_key_base + (indx * ctx->
engine->hash_pg_sz);
214 const u32 *src32 = (
const u32 *) src;
227 memcpy_toio32(key_ptr, key, key_len / 4);
228 memcpy_toio32(iv_ptr, iv, iv_len / 4);
237 const u8 *ciph_key,
size_t ciph_len,
238 const u8 *
iv,
size_t ivlen,
const u8 *hash_key,
241 unsigned indx = ctx->
engine->next_ctx++;
242 void __iomem *ciph_page_addr, *hash_page_addr;
244 ciph_page_addr = spacc_ctx_page_addr(ctx, indx, 1);
245 hash_page_addr = spacc_ctx_page_addr(ctx, indx, 0);
248 spacc_cipher_write_ctx(ctx, ciph_page_addr, ciph_key, ciph_len, iv,
255 memcpy_toio32(hash_page_addr, hash_key, hash_len / 4);
295 unsigned nents, mapped_ents;
312 ddt_set(&ddt[mapped_ents], 0, 0);
327 unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(areq));
331 int i, dst_ents, src_ents, assoc_ents;
332 u8 *iv = giv ? giv : areq->
iv;
344 req->src_ddt = src_ddt;
345 req->dst_ddt = dst_ddt;
349 if (areq->
src != areq->
dst) {
366 req->giv_pa = iv_addr;
378 ddt_set(src_ddt++, iv_addr, ivsize);
380 if (giv || req->is_encrypt)
381 ddt_set(dst_ddt++, iv_addr, ivsize);
389 if (areq->
src == areq->
dst)
398 ddt_set(src_ddt, 0, 0);
399 ddt_set(dst_ddt, 0, 0);
408 struct spacc_alg *alg = to_spacc_alg(req->req->tfm->__crt_alg);
411 unsigned ivsize = alg->
alg.cra_aead.ivsize;
414 if (areq->
src != areq->
dst) {
435 unsigned nents =
sg_count(payload, nbytes);
448 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
465 static int spacc_aead_aes_setkey(
struct crypto_aead *aead,
const u8 *key,
468 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
484 return crypto_aead_setkey(ctx->
sw_cipher, key, len);
493 static int spacc_aead_setkey(
struct crypto_aead *tfm,
const u8 *key,
498 struct rtattr *rta = (
void *)key;
518 if (keylen < enckeylen)
528 err = spacc_aead_aes_setkey(tfm, key + authkeylen, enckeylen);
530 err = spacc_aead_des_setkey(tfm, key + authkeylen, enckeylen);
545 static int spacc_aead_setauthsize(
struct crypto_aead *tfm,
546 unsigned int authsize)
548 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(crypto_aead_tfm(tfm));
560 static int spacc_aead_need_fallback(
struct spacc_req *req)
582 static int spacc_aead_do_fallback(
struct aead_request *req,
unsigned alg_type,
585 struct crypto_tfm *old_tfm = crypto_aead_tfm(crypto_aead_reqtfm(req));
595 aead_request_set_tfm(req, ctx->
sw_cipher);
596 err = is_encrypt ? crypto_aead_encrypt(req) :
597 crypto_aead_decrypt(req);
598 aead_request_set_tfm(req, __crypto_aead_cast(old_tfm));
605 static void spacc_aead_complete(
struct spacc_req *req)
607 spacc_aead_free_ddts(req);
611 static int spacc_aead_submit(
struct spacc_req *req)
616 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
633 proc_len = aead_req->
cryptlen + assoc_len;
640 assoc_len += crypto_aead_ivsize(crypto_aead_reqtfm(aead_req));
641 proc_len += crypto_aead_ivsize(crypto_aead_reqtfm(aead_req));
672 static int spacc_req_submit(
struct spacc_req *req);
678 while (!list_empty(&engine->
pending) &&
686 req->
result = spacc_req_submit(req);
698 unsigned alg_type,
bool is_encrypt)
701 struct spacc_engine *engine = to_spacc_alg(alg)->engine;
702 struct spacc_req *dev_req = aead_request_ctx(req);
705 unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
713 dev_req->
complete = spacc_aead_complete;
715 if (
unlikely(spacc_aead_need_fallback(dev_req)))
716 return spacc_aead_do_fallback(req, alg_type, is_encrypt);
718 spacc_aead_make_ddts(dev_req, dev_req->
giv);
722 if (
unlikely(spacc_fifo_cmd_full(engine)) ||
726 spin_unlock_irqrestore(&engine->
hw_lock, flags);
734 spin_unlock_irqrestore(&engine->
hw_lock, flags);
739 spacc_aead_free_ddts(dev_req);
746 struct crypto_aead *aead = crypto_aead_reqtfm(req);
747 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
748 struct spacc_alg *alg = to_spacc_alg(tfm->
__crt_alg);
750 return spacc_aead_setup(req, NULL, alg->
type, 1);
755 struct crypto_aead *tfm = aead_givcrypt_reqtfm(req);
757 size_t ivsize = crypto_aead_ivsize(tfm);
758 struct spacc_alg *alg = to_spacc_alg(tfm->
base.__crt_alg);
764 if (ivsize >
sizeof(
u64)) {
769 memcpy(req->
giv + ivsize - len, &seq, len);
771 return spacc_aead_setup(&req->
areq, req->
giv, alg->
type, 1);
776 struct crypto_aead *aead = crypto_aead_reqtfm(req);
777 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
778 struct spacc_alg *alg = to_spacc_alg(tfm->
__crt_alg);
780 return spacc_aead_setup(req, NULL, alg->
type, 0);
787 static int spacc_aead_cra_init(
struct crypto_tfm *tfm)
791 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
800 dev_warn(engine->
dev,
"failed to allocate fallback for %s\n",
809 tfm->crt_aead.reqsize =
sizeof(
struct spacc_req);
818 static void spacc_aead_cra_exit(
struct crypto_tfm *tfm)
834 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
862 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
886 err = crypto_ablkcipher_setkey(ctx->
sw_cipher, key, len);
888 goto sw_setkey_failed;
907 const u8 *key,
unsigned int len)
909 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
926 static int spacc_ablk_need_fallback(
struct spacc_req *req)
931 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
933 ctx = crypto_tfm_ctx(tfm);
941 static void spacc_ablk_complete(
struct spacc_req *req)
946 if (ablk_req->
src != ablk_req->
dst) {
958 static int spacc_ablk_submit(
struct spacc_req *req)
964 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
969 ctx->
key_len, ablk_req->
info, alg->cra_ablkcipher.ivsize,
993 unsigned alg_type,
bool is_encrypt)
996 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
1008 ablkcipher_request_set_tfm(req, ctx->
sw_cipher);
1009 err = is_encrypt ? crypto_ablkcipher_encrypt(req) :
1010 crypto_ablkcipher_decrypt(req);
1011 ablkcipher_request_set_tfm(req, __crypto_ablkcipher_cast(old_tfm));
1020 struct spacc_engine *engine = to_spacc_alg(alg)->engine;
1021 struct spacc_req *dev_req = ablkcipher_request_ctx(req);
1022 unsigned long flags;
1028 dev_req->
complete = spacc_ablk_complete;
1031 if (
unlikely(spacc_ablk_need_fallback(dev_req)))
1032 return spacc_ablk_do_fallback(req, alg_type, is_encrypt);
1038 if (req->
src != req->
dst) {
1039 dev_req->
src_ddt = spacc_sg_to_ddt(engine, req->
src,
1044 dev_req->
dst_ddt = spacc_sg_to_ddt(engine, req->
dst,
1049 dev_req->
dst_ddt = spacc_sg_to_ddt(engine, req->
dst,
1065 if (
unlikely(spacc_fifo_cmd_full(engine)) ||
1069 spin_unlock_irqrestore(&engine->
hw_lock, flags);
1077 spin_unlock_irqrestore(&engine->
hw_lock, flags);
1086 if (req->
src != req->
dst)
1093 static int spacc_ablk_cra_init(
struct crypto_tfm *tfm)
1097 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
1106 dev_warn(engine->
dev,
"failed to allocate fallback for %s\n",
1114 tfm->crt_ablkcipher.reqsize =
sizeof(
struct spacc_req);
1119 static void spacc_ablk_cra_exit(
struct crypto_tfm *tfm)
1131 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
1132 struct spacc_alg *alg = to_spacc_alg(tfm->
__crt_alg);
1134 return spacc_ablk_setup(req, alg->
type, 1);
1140 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
1141 struct spacc_alg *alg = to_spacc_alg(tfm->
__crt_alg);
1143 return spacc_ablk_setup(req, alg->
type, 0);
1146 static inline int spacc_fifo_stat_empty(
struct spacc_engine *engine)
1152 static void spacc_process_done(
struct spacc_engine *engine)
1155 unsigned long flags;
1159 while (!spacc_fifo_stat_empty(engine)) {
1182 "memory error triggered\n");
1188 "block error triggered\n");
1195 tasklet_schedule(&engine->
complete);
1197 spin_unlock_irqrestore(&engine->
hw_lock, flags);
1200 static irqreturn_t spacc_spacc_irq(
int irq,
void *dev)
1206 spacc_process_done(engine);
1211 static void spacc_packet_timeout(
unsigned long data)
1215 spacc_process_done(engine);
1218 static int spacc_req_submit(
struct spacc_req *req)
1223 return spacc_aead_submit(req);
1225 return spacc_ablk_submit(req);
1228 static void spacc_spacc_complete(
unsigned long data)
1232 unsigned long flags;
1242 spin_unlock_irqrestore(&engine->
hw_lock, flags);
1251 static int spacc_suspend(
struct device *dev)
1254 struct spacc_engine *engine = platform_get_drvdata(pdev);
1266 static int spacc_resume(
struct device *dev)
1269 struct spacc_engine *engine = platform_get_drvdata(pdev);
1274 static const struct dev_pm_ops spacc_pm_ops = {
1276 .resume = spacc_resume,
1285 static ssize_t spacc_stat_irq_thresh_show(
struct device *dev,
1289 struct spacc_engine *engine = spacc_dev_to_engine(dev);
1294 static ssize_t spacc_stat_irq_thresh_store(
struct device *dev,
1296 const char *
buf,
size_t len)
1298 struct spacc_engine *engine = spacc_dev_to_engine(dev);
1299 unsigned long thresh;
1313 spacc_stat_irq_thresh_store);
1315 static struct spacc_alg ipsec_engine_algs[] = {
1321 .cra_name =
"cbc(aes)",
1322 .cra_driver_name =
"cbc-aes-picoxcell",
1333 .setkey = spacc_aes_setkey,
1334 .encrypt = spacc_ablk_encrypt,
1335 .decrypt = spacc_ablk_decrypt,
1340 .cra_init = spacc_ablk_cra_init,
1341 .cra_exit = spacc_ablk_cra_exit,
1349 .cra_name =
"ecb(aes)",
1350 .cra_driver_name =
"ecb-aes-picoxcell",
1360 .setkey = spacc_aes_setkey,
1361 .encrypt = spacc_ablk_encrypt,
1362 .decrypt = spacc_ablk_decrypt,
1366 .cra_init = spacc_ablk_cra_init,
1367 .cra_exit = spacc_ablk_cra_exit,
1375 .cra_name =
"cbc(des)",
1376 .cra_driver_name =
"cbc-des-picoxcell",
1386 .setkey = spacc_des_setkey,
1387 .encrypt = spacc_ablk_encrypt,
1388 .decrypt = spacc_ablk_decrypt,
1393 .cra_init = spacc_ablk_cra_init,
1394 .cra_exit = spacc_ablk_cra_exit,
1402 .cra_name =
"ecb(des)",
1403 .cra_driver_name =
"ecb-des-picoxcell",
1413 .setkey = spacc_des_setkey,
1414 .encrypt = spacc_ablk_encrypt,
1415 .decrypt = spacc_ablk_decrypt,
1419 .cra_init = spacc_ablk_cra_init,
1420 .cra_exit = spacc_ablk_cra_exit,
1428 .cra_name =
"cbc(des3_ede)",
1429 .cra_driver_name =
"cbc-des3-ede-picoxcell",
1439 .setkey = spacc_des_setkey,
1440 .encrypt = spacc_ablk_encrypt,
1441 .decrypt = spacc_ablk_decrypt,
1446 .cra_init = spacc_ablk_cra_init,
1447 .cra_exit = spacc_ablk_cra_exit,
1455 .cra_name =
"ecb(des3_ede)",
1456 .cra_driver_name =
"ecb-des3-ede-picoxcell",
1466 .setkey = spacc_des_setkey,
1467 .encrypt = spacc_ablk_encrypt,
1468 .decrypt = spacc_ablk_decrypt,
1472 .cra_init = spacc_ablk_cra_init,
1473 .cra_exit = spacc_ablk_cra_exit,
1482 .cra_name =
"authenc(hmac(sha1),cbc(aes))",
1483 .cra_driver_name =
"authenc-hmac-sha1-cbc-aes-picoxcell",
1493 .setkey = spacc_aead_setkey,
1494 .setauthsize = spacc_aead_setauthsize,
1495 .encrypt = spacc_aead_encrypt,
1496 .decrypt = spacc_aead_decrypt,
1497 .givencrypt = spacc_aead_givencrypt,
1501 .cra_init = spacc_aead_cra_init,
1502 .cra_exit = spacc_aead_cra_exit,
1512 .cra_name =
"authenc(hmac(sha256),cbc(aes))",
1513 .cra_driver_name =
"authenc-hmac-sha256-cbc-aes-picoxcell",
1523 .setkey = spacc_aead_setkey,
1524 .setauthsize = spacc_aead_setauthsize,
1525 .encrypt = spacc_aead_encrypt,
1526 .decrypt = spacc_aead_decrypt,
1527 .givencrypt = spacc_aead_givencrypt,
1531 .cra_init = spacc_aead_cra_init,
1532 .cra_exit = spacc_aead_cra_exit,
1541 .cra_name =
"authenc(hmac(md5),cbc(aes))",
1542 .cra_driver_name =
"authenc-hmac-md5-cbc-aes-picoxcell",
1552 .setkey = spacc_aead_setkey,
1553 .setauthsize = spacc_aead_setauthsize,
1554 .encrypt = spacc_aead_encrypt,
1555 .decrypt = spacc_aead_decrypt,
1556 .givencrypt = spacc_aead_givencrypt,
1560 .cra_init = spacc_aead_cra_init,
1561 .cra_exit = spacc_aead_cra_exit,
1570 .cra_name =
"authenc(hmac(sha1),cbc(des3_ede))",
1571 .cra_driver_name =
"authenc-hmac-sha1-cbc-3des-picoxcell",
1581 .setkey = spacc_aead_setkey,
1582 .setauthsize = spacc_aead_setauthsize,
1583 .encrypt = spacc_aead_encrypt,
1584 .decrypt = spacc_aead_decrypt,
1585 .givencrypt = spacc_aead_givencrypt,
1589 .cra_init = spacc_aead_cra_init,
1590 .cra_exit = spacc_aead_cra_exit,
1600 .cra_name =
"authenc(hmac(sha256),cbc(des3_ede))",
1601 .cra_driver_name =
"authenc-hmac-sha256-cbc-3des-picoxcell",
1611 .setkey = spacc_aead_setkey,
1612 .setauthsize = spacc_aead_setauthsize,
1613 .encrypt = spacc_aead_encrypt,
1614 .decrypt = spacc_aead_decrypt,
1615 .givencrypt = spacc_aead_givencrypt,
1619 .cra_init = spacc_aead_cra_init,
1620 .cra_exit = spacc_aead_cra_exit,
1629 .cra_name =
"authenc(hmac(md5),cbc(des3_ede))",
1630 .cra_driver_name =
"authenc-hmac-md5-cbc-3des-picoxcell",
1640 .setkey = spacc_aead_setkey,
1641 .setauthsize = spacc_aead_setauthsize,
1642 .encrypt = spacc_aead_encrypt,
1643 .decrypt = spacc_aead_decrypt,
1644 .givencrypt = spacc_aead_givencrypt,
1648 .cra_init = spacc_aead_cra_init,
1649 .cra_exit = spacc_aead_cra_exit,
1654 static struct spacc_alg l2_engine_algs[] = {
1661 .cra_name =
"f8(kasumi)",
1662 .cra_driver_name =
"f8-kasumi-picoxcell",
1672 .setkey = spacc_kasumi_f8_setkey,
1673 .encrypt = spacc_ablk_encrypt,
1674 .decrypt = spacc_ablk_decrypt,
1679 .cra_init = spacc_ablk_cra_init,
1680 .cra_exit = spacc_ablk_cra_exit,
1687 { .compatible =
"picochip,spacc-ipsec" },
1688 { .compatible =
"picochip,spacc-l2" },
1692 #define spacc_of_id_table NULL
1696 const char *spacc_type)
1700 if (platid && !
strcmp(platid->
name, spacc_type))
1720 if (spacc_is_compatible(pdev,
"picochip,spacc-ipsec")) {
1725 engine->
algs = ipsec_engine_algs;
1727 }
else if (spacc_is_compatible(pdev,
"picochip,spacc-l2")) {
1732 engine->
algs = l2_engine_algs;
1738 engine->
name = dev_name(&pdev->
dev);
1743 dev_err(&pdev->
dev,
"no memory/irq resource for engine\n");
1752 if (!engine->
regs) {
1757 if (devm_request_irq(&pdev->
dev, irq->
start, spacc_spacc_irq, 0,
1758 engine->
name, engine)) {
1759 dev_err(engine->
dev,
"failed to request IRQ\n");
1763 engine->
dev = &pdev->
dev;
1775 if (IS_ERR(engine->
clk)) {
1778 return PTR_ERR(engine->
clk);
1813 (
unsigned long)engine);
1815 INIT_LIST_HEAD(&engine->
pending);
1820 (
unsigned long)engine);
1822 platform_set_drvdata(pdev, engine);
1825 for (i = 0; i < engine->
num_algs; ++
i) {
1826 engine->
algs[
i].engine = engine;
1834 dev_err(engine->
dev,
"failed to register alg \"%s\"\n",
1835 engine->
algs[i].alg.cra_name);
1837 dev_dbg(engine->
dev,
"registered alg \"%s\"\n",
1838 engine->
algs[i].alg.cra_name);
1847 struct spacc_engine *engine = platform_get_drvdata(pdev);
1864 {
"picochip,spacc-ipsec", },
1865 {
"picochip,spacc-l2", },
1869 .probe = spacc_probe,
1872 .name =
"picochip,spacc",
1874 .pm = &spacc_pm_ops,
1878 .id_table = spacc_id_table,