6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/kernel.h>
9 #include <linux/module.h>
13 #include <linux/slab.h>
17 #include <crypto/sha.h>
18 #include <crypto/aes.h>
22 #include <linux/sched.h>
28 #include <asm/hypervisor.h>
33 #define DRV_MODULE_NAME "n2_crypto"
34 #define DRV_MODULE_VERSION "0.2"
35 #define DRV_MODULE_RELDATE "July 28, 2011"
45 #define N2_CRA_PRIORITY 200
71 static unsigned long spu_next_offset(
struct spu_queue *
q,
unsigned long off)
89 #define OFFSET_NOT_RUNNING (~(unsigned int)0)
95 static inline bool job_finished(
struct spu_queue *q,
unsigned int offset,
96 unsigned long old_head,
unsigned long new_head)
98 if (old_head <= new_head) {
99 if (offset > old_head && offset <= new_head)
102 if (offset > old_head || offset <= new_head)
115 unsigned long off, new_head, hv_ret;
118 pr_err(
"CPU[%d]: Got CWQ interrupt for qhdl[%lx]\n",
125 pr_err(
"CPU[%d]: CWQ gethead[%lx] hv_ret[%lu]\n",
128 for (off = q->
head; off != new_head; off = spu_next_offset(q, off)) {
136 spin_unlock(&q->
lock);
144 unsigned long head, hv_ret;
148 pr_err(
"CPU[%d]: Got MAU interrupt for qhdl[%lx]\n",
153 pr_err(
"CPU[%d]: MAU gethead[%lx] hv_ret[%lu]\n",
158 spin_unlock(&q->
lock);
163 static void *spu_queue_next(
struct spu_queue *q,
void *
cur)
165 return q->
q + spu_next_offset(q, cur - q->
q);
168 static int spu_queue_num_free(
struct spu_queue *q)
170 unsigned long head = q->
head;
178 diff = (end -
tail) + head;
185 int avail = spu_queue_num_free(q);
187 if (avail >= num_entries)
188 return q->
q + q->
tail;
193 static unsigned long spu_queue_submit(
struct spu_queue *q,
void *last)
195 unsigned long hv_ret, new_tail;
197 new_tail = spu_next_offset(q, last - q->
q);
205 static u64 control_word_base(
unsigned int len,
unsigned int hmac_key_len,
206 int enc_type,
int auth_type,
207 unsigned int hash_len,
208 bool sfas,
bool sob,
bool eob,
bool encrypt,
233 static inline bool n2_should_run_async(
struct spu_queue *qp,
int this_len)
235 if (this_len >= 64 ||
282 #define N2_HASH_KEY_MAX 32
357 static int n2_hash_cra_init(
struct crypto_tfm *tfm)
359 const char *fallback_driver_name = tfm->
__crt_alg->cra_name;
367 if (IS_ERR(fallback_tfm)) {
368 pr_warning(
"Fallback driver '%s' could not be loaded!\n",
369 fallback_driver_name);
370 err = PTR_ERR(fallback_tfm);
375 crypto_ahash_reqsize(fallback_tfm)));
384 static void n2_hash_cra_exit(
struct crypto_tfm *tfm)
392 static int n2_hmac_cra_init(
struct crypto_tfm *tfm)
394 const char *fallback_driver_name = tfm->
__crt_alg->cra_name;
404 if (IS_ERR(fallback_tfm)) {
405 pr_warning(
"Fallback driver '%s' could not be loaded!\n",
406 fallback_driver_name);
407 err = PTR_ERR(fallback_tfm);
412 if (IS_ERR(child_shash)) {
413 pr_warning(
"Child shash '%s' could not be loaded!\n",
415 err = PTR_ERR(child_shash);
416 goto out_free_fallback;
420 crypto_ahash_reqsize(fallback_tfm)));
423 ctx->
base.fallback_tfm = fallback_tfm;
427 crypto_free_ahash(fallback_tfm);
433 static void n2_hmac_cra_exit(
struct crypto_tfm *tfm)
438 crypto_free_ahash(ctx->
base.fallback_tfm);
450 char ctx[crypto_shash_descsize(child_shash)];
454 fallback_tfm = ctx->
base.fallback_tfm;
459 desc.shash.tfm = child_shash;
460 desc.shash.
flags = crypto_ahash_get_flags(tfm) &
463 bs = crypto_shash_blocksize(child_shash);
464 ds = crypto_shash_digestsize(child_shash);
480 static unsigned long wait_for_tail(
struct spu_queue *qp)
482 unsigned long head, hv_ret;
487 pr_err(
"Hypervisor error on gethead\n");
490 if (head == qp->
tail) {
498 static unsigned long submit_and_wait_for_tail(
struct spu_queue *qp,
501 unsigned long hv_ret = spu_queue_submit(qp, ent);
504 hv_ret = wait_for_tail(qp);
510 unsigned int auth_type,
unsigned int digest_size,
512 unsigned long auth_key,
unsigned int auth_key_len)
542 qp = cpu_to_cwq[
cpu];
551 ent = qp->
q + qp->
tail;
553 ent->
control = control_word_base(nbytes, auth_key_len, 0,
554 auth_type, digest_size,
555 false,
true,
false,
false,
568 ent = spu_queue_next(qp, ent);
583 if (submit_and_wait_for_tail(qp, ent) !=
HV_EOK)
588 spin_unlock_irqrestore(&qp->
lock, flags);
611 return n2_do_async_digest(req, n2alg->
auth_type,
624 ds = n2alg->
derived.digest_size;
642 return n2_do_async_digest(req, n2alg->
derived.hmac_type,
643 n2alg->
derived.hw_op_hashsz, ds,
660 #define N2_CHUNK_ARR_LEN 16
720 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
749 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
776 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
794 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
807 for (i = 0; i < 256; i++)
809 for (i = 0; i < 256; i++) {
811 j = (j + key[
k] +
a) & 0xff;
821 static inline int cipher_descriptor_len(
int nbytes,
unsigned int block_size)
825 this_len -= (nbytes & (block_size - 1));
826 return this_len > (1 << 16) ? (1 << 16) : this_len;
837 ent = spu_queue_alloc(qp, cp->
arr_len);
839 pr_info(
"queue_alloc() of %d fails\n",
846 ent->
control = control_word_base(cp->
arr[0].src_len,
848 false,
true,
false, encrypt,
859 for (i = 1; i < cp->
arr_len; i++) {
860 ent = spu_queue_next(qp, ent);
873 return (spu_queue_submit(qp, ent) !=
HV_EOK) ? -
EINVAL : 0;
881 unsigned long dest_prev;
882 unsigned int tot_len;
886 ablkcipher_walk_init(walk, req->
dst, req->
src, req->
nbytes);
893 chunk = &rctx->
chunk;
894 INIT_LIST_HEAD(&chunk->
entry);
900 prev_in_place =
false;
904 while ((nbytes = walk->
nbytes) != 0) {
914 this_len = cipher_descriptor_len(nbytes, walk->
blocksize);
917 if (in_place != prev_in_place ||
919 dest_paddr != dest_prev) ||
921 tot_len + this_len > (1 << 16)) {
930 INIT_LIST_HEAD(&chunk->
entry);
938 chunk->
arr[chunk->
arr_len].src_len = this_len;
941 dest_prev = dest_paddr + this_len;
942 prev_in_place = in_place;
949 if (!err && chunk->
arr_len != 0) {
965 ablkcipher_walk_complete(&rctx->
walk);
978 int err = n2_compute_chunks(req);
980 unsigned long flags, hv_ret;
994 err = __n2_crypt_chunk(tfm, c, qp, encrypt);
1002 hv_ret = wait_for_tail(qp);
1007 spin_unlock_irqrestore(&qp->
lock, flags);
1012 n2_chunk_complete(req,
NULL);
1018 return n2_do_ecb(req,
true);
1023 return n2_do_ecb(req,
false);
1030 unsigned long flags, hv_ret, iv_paddr;
1031 int err = n2_compute_chunks(req);
1034 void *final_iv_addr;
1036 final_iv_addr =
NULL;
1053 err = __n2_crypt_chunk(tfm, c, qp,
true);
1061 final_iv_addr =
__va(iv_paddr);
1065 if (c == &rctx->
chunk) {
1068 iv_paddr = (tmp->
arr[tmp->
arr_len-1].src_paddr +
1070 rctx->
walk.blocksize);
1072 if (!final_iv_addr) {
1077 rctx->
walk.blocksize);
1078 final_iv_addr = rctx->
temp_iv;
1080 rctx->
walk.blocksize);
1083 err = __n2_crypt_chunk(tfm, c, qp,
false);
1092 hv_ret = wait_for_tail(qp);
1097 spin_unlock_irqrestore(&qp->
lock, flags);
1102 n2_chunk_complete(req, err ?
NULL : final_iv_addr);
1108 return n2_do_chaining(req,
true);
1113 return n2_do_chaining(req,
false);
1126 { .name =
"ecb(arc4)",
1127 .drv_name =
"ecb-arc4",
1134 .setkey = n2_arc4_setkey,
1135 .encrypt = n2_encrypt_ecb,
1136 .decrypt = n2_decrypt_ecb,
1141 { .name =
"ecb(des)",
1142 .drv_name =
"ecb-des",
1149 .setkey = n2_des_setkey,
1150 .encrypt = n2_encrypt_ecb,
1151 .decrypt = n2_decrypt_ecb,
1154 { .name =
"cbc(des)",
1155 .drv_name =
"cbc-des",
1163 .setkey = n2_des_setkey,
1164 .encrypt = n2_encrypt_chaining,
1165 .decrypt = n2_decrypt_chaining,
1168 { .name =
"cfb(des)",
1169 .drv_name =
"cfb-des",
1176 .setkey = n2_des_setkey,
1177 .encrypt = n2_encrypt_chaining,
1178 .decrypt = n2_decrypt_chaining,
1183 { .name =
"ecb(des3_ede)",
1184 .drv_name =
"ecb-3des",
1191 .setkey = n2_3des_setkey,
1192 .encrypt = n2_encrypt_ecb,
1193 .decrypt = n2_decrypt_ecb,
1196 { .name =
"cbc(des3_ede)",
1197 .drv_name =
"cbc-3des",
1205 .setkey = n2_3des_setkey,
1206 .encrypt = n2_encrypt_chaining,
1207 .decrypt = n2_decrypt_chaining,
1210 { .name =
"cfb(des3_ede)",
1211 .drv_name =
"cfb-3des",
1218 .setkey = n2_3des_setkey,
1219 .encrypt = n2_encrypt_chaining,
1220 .decrypt = n2_decrypt_chaining,
1224 { .name =
"ecb(aes)",
1225 .drv_name =
"ecb-aes",
1232 .setkey = n2_aes_setkey,
1233 .encrypt = n2_encrypt_ecb,
1234 .decrypt = n2_decrypt_ecb,
1237 { .name =
"cbc(aes)",
1238 .drv_name =
"cbc-aes",
1246 .setkey = n2_aes_setkey,
1247 .encrypt = n2_encrypt_chaining,
1248 .decrypt = n2_decrypt_chaining,
1251 { .name =
"ctr(aes)",
1252 .drv_name =
"ctr-aes",
1260 .setkey = n2_aes_setkey,
1261 .encrypt = n2_encrypt_chaining,
1262 .decrypt = n2_encrypt_chaining,
1267 #define NUM_CIPHER_TMPLS ARRAY_SIZE(cipher_tmpls)
1283 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
1284 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e,
1293 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32,
1294 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8,
1301 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a,
1302 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae,
1303 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99,
1304 0x1b, 0x78, 0x52, 0xb8, 0x55
1311 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
1312 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
1313 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
1323 .hash_zero = md5_zero,
1324 .hash_init = md5_init,
1331 .hash_zero = sha1_zero,
1332 .hash_init = sha1_init,
1339 .hash_zero = sha256_zero,
1340 .hash_init = sha256_init,
1347 .hash_zero = sha224_zero,
1348 .hash_init = sha224_init,
1355 #define NUM_HASH_TMPLS ARRAY_SIZE(hash_tmpls)
1360 static int algs_registered;
1362 static void __n2_unregister_algs(
void)
1385 static int n2_cipher_cra_init(
struct crypto_tfm *tfm)
1412 alg->
cra_init = n2_cipher_cra_init;
1415 list_add(&p->
entry, &cipher_algs);
1439 INIT_LIST_HEAD(&p->
derived.entry);
1442 ahash->
digest = n2_hmac_async_digest;
1443 ahash->
setkey = n2_hmac_async_setkey;
1445 base = &ahash->
halg.base;
1453 list_add(&p->
derived.entry, &hmac_algs);
1484 ahash->
init = n2_hash_async_init;
1485 ahash->
update = n2_hash_async_update;
1486 ahash->
final = n2_hash_async_final;
1487 ahash->
finup = n2_hash_async_finup;
1488 ahash->
digest = n2_hash_async_digest;
1490 halg = &ahash->
halg;
1506 list_add(&p->
entry, &ahash_algs);
1516 err = __n2_register_one_hmac(p);
1520 static int __devinit n2_register_algs(
void)
1525 if (algs_registered++)
1529 err = __n2_register_one_ahash(&hash_tmpls[i]);
1531 __n2_unregister_algs();
1536 err = __n2_register_one_cipher(&cipher_tmpls[i]);
1538 __n2_unregister_algs();
1548 static void __devexit n2_unregister_algs(
void)
1551 if (!--algs_registered)
1552 __n2_unregister_algs();
1569 const unsigned int *dev_intrs;
1586 for (i = 0; i < dev->
archdata.num_irqs; i++) {
1587 if (dev_intrs[i] == intr)
1595 const char *irq_name,
struct spu_queue *p,
1605 index = find_devino_index(dev, ip, p->
devino);
1618 static void *new_queue(
unsigned long q_type)
1620 return kmem_cache_zalloc(queue_cache[q_type - 1],
GFP_KERNEL);
1623 static void free_queue(
void *p,
unsigned long q_type)
1628 static int queue_cache_init(
void)
1635 MAU_ENTRY_SIZE, 0,
NULL);
1644 CWQ_ENTRY_SIZE, 0,
NULL);
1652 static void queue_cache_destroy(
void)
1658 static int spu_queue_register(
struct spu_queue *p,
unsigned long q_type)
1661 unsigned long hv_ret;
1663 if (cpumask_empty(&p->
sharing))
1666 if (!alloc_cpumask_var(&old_allowed,
GFP_KERNEL))
1669 cpumask_copy(old_allowed, &
current->cpus_allowed);
1678 set_cpus_allowed_ptr(
current, old_allowed);
1680 free_cpumask_var(old_allowed);
1682 return (hv_ret ? -
EINVAL : 0);
1685 static int spu_queue_setup(
struct spu_queue *p)
1693 err = spu_queue_register(p, p->
q_type);
1702 static void spu_queue_destroy(
struct spu_queue *p)
1704 unsigned long hv_ret;
1722 for (i = 0; i <
NR_CPUS; i++) {
1723 if (cpu_to_cwq[i] == p)
1724 cpu_to_cwq[
i] =
NULL;
1731 spu_queue_destroy(p);
1755 if (table[*
id] !=
NULL) {
1756 dev_err(&dev->
dev,
"%s: SPU cpu slot already set.\n",
1757 dev->
dev.of_node->full_name);
1769 u64 node,
const char *iname,
unsigned long q_type,
1777 dev_err(&dev->
dev,
"%s: Could not allocate SPU queue.\n",
1778 dev->
dev.of_node->full_name);
1785 INIT_LIST_HEAD(&p->
jobs);
1786 list_add(&p->
list, list);
1788 err = spu_mdesc_walk_arcs(mdesc, dev, node, p, table);
1792 err = spu_queue_setup(p);
1796 return spu_map_ino(dev, ip, iname, p, handler);
1801 const char *exec_name,
unsigned long q_type,
1811 if (!type ||
strcmp(type, exec_name))
1814 err = handle_exec_unit(ip, list, dev, mdesc, node,
1815 exec_name, q_type, handler, table);
1817 spu_list_destroy(list);
1859 const unsigned int *
reg;
1871 if (!name ||
strcmp(name, node_name))
1874 if (!chdl || (*chdl != *reg))
1877 return get_irq_props(mdesc, node, ip);
1883 static unsigned long n2_spu_hvapi_major;
1884 static unsigned long n2_spu_hvapi_minor;
1886 static int __devinit n2_spu_hvapi_register(
void)
1890 n2_spu_hvapi_major = 2;
1891 n2_spu_hvapi_minor = 0;
1895 &n2_spu_hvapi_minor);
1898 pr_info(
"Registered NCS HVAPI version %lu.%lu\n",
1900 n2_spu_hvapi_minor);
1905 static void n2_spu_hvapi_unregister(
void)
1910 static int global_ref;
1912 static int __devinit grab_global_resources(
void)
1921 err = n2_spu_hvapi_register();
1925 err = queue_cache_init();
1927 goto out_hvapi_release;
1930 cpu_to_cwq = kzalloc(
sizeof(
struct spu_queue *) * NR_CPUS,
1933 goto out_queue_cache_destroy;
1935 cpu_to_mau = kzalloc(
sizeof(
struct spu_queue *) * NR_CPUS,
1938 goto out_free_cwq_table;
1952 out_queue_cache_destroy:
1953 queue_cache_destroy();
1956 n2_spu_hvapi_unregister();
1960 static void release_global_resources(
void)
1963 if (!--global_ref) {
1970 queue_cache_destroy();
1971 n2_spu_hvapi_unregister();
1986 static void free_n2cp(
struct n2_crypto *np)
1996 static void __devinit n2_spu_driver_version(
void)
1998 static int n2_spu_version_printed;
2000 if (n2_spu_version_printed++ == 0)
2007 const char *full_name;
2011 n2_spu_driver_version();
2013 full_name = dev->
dev.of_node->full_name;
2014 pr_info(
"Found N2CP at %s\n", full_name);
2018 dev_err(&dev->
dev,
"%s: Unable to allocate n2cp.\n",
2023 err = grab_global_resources();
2026 "global resources.\n", full_name);
2033 dev_err(&dev->
dev,
"%s: Unable to grab MDESC.\n",
2036 goto out_free_global;
2038 err = grab_mdesc_irq_props(mdesc, dev, &np->
cwq_info,
"n2cp");
2040 dev_err(&dev->
dev,
"%s: Unable to grab IRQ props.\n",
2043 goto out_free_global;
2052 dev_err(&dev->
dev,
"%s: CWQ MDESC scan failed.\n",
2054 goto out_free_global;
2057 err = n2_register_algs();
2059 dev_err(&dev->
dev,
"%s: Unable to register algorithms.\n",
2061 goto out_free_spu_list;
2072 release_global_resources();
2084 n2_unregister_algs();
2088 release_global_resources();
2105 static void free_ncp(
struct n2_mau *mp)
2118 const char *full_name;
2122 n2_spu_driver_version();
2124 full_name = dev->
dev.of_node->full_name;
2125 pr_info(
"Found NCP at %s\n", full_name);
2129 dev_err(&dev->
dev,
"%s: Unable to allocate ncp.\n",
2134 err = grab_global_resources();
2137 "global resources.\n", full_name);
2144 dev_err(&dev->
dev,
"%s: Unable to grab MDESC.\n",
2147 goto out_free_global;
2150 err = grab_mdesc_irq_props(mdesc, dev, &mp->
mau_info,
"ncp");
2152 dev_err(&dev->
dev,
"%s: Unable to grab IRQ props.\n",
2155 goto out_free_global;
2164 dev_err(&dev->
dev,
"%s: MAU MDESC scan failed.\n",
2166 goto out_free_global;
2174 release_global_resources();
2188 release_global_resources();
2198 .compatible =
"SUNW,n2-cwq",
2202 .compatible =
"SUNW,vf-cwq",
2206 .compatible =
"SUNW,kt-cwq",
2217 .of_match_table = n2_crypto_match,
2219 .probe = n2_crypto_probe,
2226 .compatible =
"SUNW,n2-mau",
2230 .compatible =
"SUNW,vf-mau",
2234 .compatible =
"SUNW,kt-mau",
2245 .of_match_table = n2_mau_match,
2247 .probe = n2_mau_probe,
2251 static int __init n2_init(
void)
2263 static void __exit n2_exit(
void)