271-crypto-add-ss.patch 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  1. diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig
  2. index 2fb0fdf..9ba9759 100644
  3. --- a/drivers/crypto/Kconfig
  4. +++ b/drivers/crypto/Kconfig
  5. @@ -436,4 +436,21 @@ config CRYPTO_DEV_QCE
  6. hardware. To compile this driver as a module, choose M here. The
  7. module will be called qcrypto.
  8. +config CRYPTO_DEV_SUNXI_SS
  9. + tristate "Support for Allwinner Security System cryptographic accelerator"
  10. + depends on ARCH_SUNXI
  11. + select CRYPTO_MD5
  12. + select CRYPTO_SHA1
  13. + select CRYPTO_AES
  14. + select CRYPTO_DES
  15. + select CRYPTO_BLKCIPHER
  16. + help
  17. + Some Allwinner SoC have a crypto accelerator named
  18. + Security System. Select this if you want to use it.
  19. + The Security System handle AES/DES/3DES ciphers in CBC mode
  20. + and SHA1 and MD5 hash algorithms.
  21. +
  22. + To compile this driver as a module, choose M here: the module
  23. + will be called sunxi-ss.
  24. +
  25. endif # CRYPTO_HW
  26. diff --git a/drivers/crypto/Makefile b/drivers/crypto/Makefile
  27. index 3924f93..856545c 100644
  28. --- a/drivers/crypto/Makefile
  29. +++ b/drivers/crypto/Makefile
  30. @@ -25,3 +25,4 @@ obj-$(CONFIG_CRYPTO_DEV_TALITOS) += talitos.o
  31. obj-$(CONFIG_CRYPTO_DEV_UX500) += ux500/
  32. obj-$(CONFIG_CRYPTO_DEV_QAT) += qat/
  33. obj-$(CONFIG_CRYPTO_DEV_QCE) += qce/
  34. +obj-$(CONFIG_CRYPTO_DEV_SUNXI_SS) += sunxi-ss/
  35. diff --git a/drivers/crypto/sunxi-ss/Makefile b/drivers/crypto/sunxi-ss/Makefile
  36. new file mode 100644
  37. index 0000000..8bb287d
  38. --- /dev/null
  39. +++ b/drivers/crypto/sunxi-ss/Makefile
  40. @@ -0,0 +1,2 @@
  41. +obj-$(CONFIG_CRYPTO_DEV_SUNXI_SS) += sunxi-ss.o
  42. +sunxi-ss-y += sunxi-ss-core.o sunxi-ss-hash.o sunxi-ss-cipher.o
  43. diff --git a/drivers/crypto/sunxi-ss/sunxi-ss-cipher.c b/drivers/crypto/sunxi-ss/sunxi-ss-cipher.c
  44. new file mode 100644
  45. index 0000000..8d0416e
  46. --- /dev/null
  47. +++ b/drivers/crypto/sunxi-ss/sunxi-ss-cipher.c
  48. @@ -0,0 +1,489 @@
  49. +/*
  50. + * sunxi-ss-cipher.c - hardware cryptographic accelerator for Allwinner A20 SoC
  51. + *
  52. + * Copyright (C) 2013-2014 Corentin LABBE <clabbe.montjoie@gmail.com>
  53. + *
  54. + * This file add support for AES cipher with 128,192,256 bits
  55. + * keysize in CBC mode.
  56. + * Add support also for DES and 3DES in CBC mode.
  57. + *
  58. + * You could find the datasheet in Documentation/arm/sunxi/README
  59. + *
  60. + * This program is free software; you can redistribute it and/or modify
  61. + * it under the terms of the GNU General Public License as published by
  62. + * the Free Software Foundation; either version 2 of the License, or
  63. + * (at your option) any later version.
  64. + */
  65. +#include "sunxi-ss.h"
  66. +
  67. +extern struct sunxi_ss_ctx *ss;
  68. +
  69. +static int sunxi_ss_cipher(struct ablkcipher_request *areq, u32 mode)
  70. +{
  71. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  72. + struct sunxi_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  73. + const char *cipher_type;
  74. +
  75. + if (areq->nbytes == 0)
  76. + return 0;
  77. +
  78. + if (areq->info == NULL) {
  79. + dev_err(ss->dev, "ERROR: Empty IV\n");
  80. + return -EINVAL;
  81. + }
  82. +
  83. + if (areq->src == NULL || areq->dst == NULL) {
  84. + dev_err(ss->dev, "ERROR: Some SGs are NULL\n");
  85. + return -EINVAL;
  86. + }
  87. +
  88. + cipher_type = crypto_tfm_alg_name(crypto_ablkcipher_tfm(tfm));
  89. +
  90. + if (strcmp("cbc(aes)", cipher_type) == 0) {
  91. + mode |= SS_OP_AES | SS_CBC | SS_ENABLED | op->keymode;
  92. + return sunxi_ss_aes_poll(areq, mode);
  93. + }
  94. +
  95. + if (strcmp("cbc(des)", cipher_type) == 0) {
  96. + mode |= SS_OP_DES | SS_CBC | SS_ENABLED | op->keymode;
  97. + return sunxi_ss_des_poll(areq, mode);
  98. + }
  99. +
  100. + if (strcmp("cbc(des3_ede)", cipher_type) == 0) {
  101. + mode |= SS_OP_3DES | SS_CBC | SS_ENABLED | op->keymode;
  102. + return sunxi_ss_des_poll(areq, mode);
  103. + }
  104. +
  105. + dev_err(ss->dev, "ERROR: Cipher %s not handled\n", cipher_type);
  106. + return -EINVAL;
  107. +}
  108. +
  109. +int sunxi_ss_cipher_encrypt(struct ablkcipher_request *areq)
  110. +{
  111. + return sunxi_ss_cipher(areq, SS_ENCRYPTION);
  112. +}
  113. +
  114. +int sunxi_ss_cipher_decrypt(struct ablkcipher_request *areq)
  115. +{
  116. + return sunxi_ss_cipher(areq, SS_DECRYPTION);
  117. +}
  118. +
  119. +int sunxi_ss_cipher_init(struct crypto_tfm *tfm)
  120. +{
  121. + struct sunxi_tfm_ctx *op = crypto_tfm_ctx(tfm);
  122. +
  123. + memset(op, 0, sizeof(struct sunxi_tfm_ctx));
  124. + return 0;
  125. +}
  126. +
  127. +/*
  128. + * Optimized function for the case where we have only one SG,
  129. + * so we can use kmap_atomic
  130. + */
  131. +static int sunxi_ss_aes_poll_atomic(struct ablkcipher_request *areq)
  132. +{
  133. + u32 spaces;
  134. + struct scatterlist *in_sg = areq->src;
  135. + struct scatterlist *out_sg = areq->dst;
  136. + void *src_addr;
  137. + void *dst_addr;
  138. + unsigned int ileft = areq->nbytes;
  139. + unsigned int oleft = areq->nbytes;
  140. + unsigned int todo;
  141. + u32 *src32;
  142. + u32 *dst32;
  143. + u32 rx_cnt = 32;
  144. + u32 tx_cnt = 0;
  145. + int i;
  146. +
  147. + src_addr = kmap_atomic(sg_page(in_sg)) + in_sg->offset;
  148. + if (src_addr == NULL) {
  149. + dev_err(ss->dev, "kmap_atomic error for src SG\n");
  150. + writel(0, ss->base + SS_CTL);
  151. + mutex_unlock(&ss->lock);
  152. + return -EINVAL;
  153. + }
  154. +
  155. + dst_addr = kmap_atomic(sg_page(out_sg)) + out_sg->offset;
  156. + if (dst_addr == NULL) {
  157. + dev_err(ss->dev, "kmap_atomic error for dst SG\n");
  158. + writel(0, ss->base + SS_CTL);
  159. + kunmap_atomic(src_addr);
  160. + mutex_unlock(&ss->lock);
  161. + return -EINVAL;
  162. + }
  163. +
  164. + src32 = (u32 *)src_addr;
  165. + dst32 = (u32 *)dst_addr;
  166. + ileft = areq->nbytes / 4;
  167. + oleft = areq->nbytes / 4;
  168. + i = 0;
  169. + do {
  170. + if (ileft > 0 && rx_cnt > 0) {
  171. + todo = min(rx_cnt, ileft);
  172. + ileft -= todo;
  173. + do {
  174. + writel_relaxed(*src32++,
  175. + ss->base +
  176. + SS_RXFIFO);
  177. + todo--;
  178. + } while (todo > 0);
  179. + }
  180. + if (tx_cnt > 0) {
  181. + todo = min(tx_cnt, oleft);
  182. + oleft -= todo;
  183. + do {
  184. + *dst32++ = readl_relaxed(ss->base +
  185. + SS_TXFIFO);
  186. + todo--;
  187. + } while (todo > 0);
  188. + }
  189. + spaces = readl_relaxed(ss->base + SS_FCSR);
  190. + rx_cnt = SS_RXFIFO_SPACES(spaces);
  191. + tx_cnt = SS_TXFIFO_SPACES(spaces);
  192. + } while (oleft > 0);
  193. + writel(0, ss->base + SS_CTL);
  194. + kunmap_atomic(src_addr);
  195. + kunmap_atomic(dst_addr);
  196. + mutex_unlock(&ss->lock);
  197. + return 0;
  198. +}
  199. +
  200. +int sunxi_ss_aes_poll(struct ablkcipher_request *areq, u32 mode)
  201. +{
  202. + u32 spaces;
  203. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  204. + struct sunxi_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  205. + unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
  206. + /* when activating SS, the default FIFO space is 32 */
  207. + u32 rx_cnt = 32;
  208. + u32 tx_cnt = 0;
  209. + u32 v;
  210. + int i;
  211. + struct scatterlist *in_sg = areq->src;
  212. + struct scatterlist *out_sg = areq->dst;
  213. + void *src_addr;
  214. + void *dst_addr;
  215. + unsigned int ileft = areq->nbytes;
  216. + unsigned int oleft = areq->nbytes;
  217. + unsigned int sgileft = areq->src->length;
  218. + unsigned int sgoleft = areq->dst->length;
  219. + unsigned int todo;
  220. + u32 *src32;
  221. + u32 *dst32;
  222. +
  223. + mutex_lock(&ss->lock);
  224. +
  225. + for (i = 0; i < op->keylen; i += 4)
  226. + writel(*(op->key + i/4), ss->base + SS_KEY0 + i);
  227. +
  228. + if (areq->info != NULL) {
  229. + for (i = 0; i < 4 && i < ivsize / 4; i++) {
  230. + v = *(u32 *)(areq->info + i * 4);
  231. + writel(v, ss->base + SS_IV0 + i * 4);
  232. + }
  233. + }
  234. + writel(mode, ss->base + SS_CTL);
  235. +
  236. + /* If we have only one SG, we can use kmap_atomic */
  237. + if (sg_next(in_sg) == NULL && sg_next(out_sg) == NULL)
  238. + return sunxi_ss_aes_poll_atomic(areq);
  239. +
  240. + /*
  241. + * If we have more than one SG, we cannot use kmap_atomic since
  242. + * we hold the mapping too long
  243. + */
  244. + src_addr = kmap(sg_page(in_sg)) + in_sg->offset;
  245. + if (src_addr == NULL) {
  246. + dev_err(ss->dev, "KMAP error for src SG\n");
  247. + mutex_unlock(&ss->lock);
  248. + return -EINVAL;
  249. + }
  250. + dst_addr = kmap(sg_page(out_sg)) + out_sg->offset;
  251. + if (dst_addr == NULL) {
  252. + kunmap(sg_page(in_sg));
  253. + dev_err(ss->dev, "KMAP error for dst SG\n");
  254. + mutex_unlock(&ss->lock);
  255. + return -EINVAL;
  256. + }
  257. + src32 = (u32 *)src_addr;
  258. + dst32 = (u32 *)dst_addr;
  259. + ileft = areq->nbytes / 4;
  260. + oleft = areq->nbytes / 4;
  261. + sgileft = in_sg->length / 4;
  262. + sgoleft = out_sg->length / 4;
  263. + do {
  264. + spaces = readl_relaxed(ss->base + SS_FCSR);
  265. + rx_cnt = SS_RXFIFO_SPACES(spaces);
  266. + tx_cnt = SS_TXFIFO_SPACES(spaces);
  267. + todo = min3(rx_cnt, ileft, sgileft);
  268. + if (todo > 0) {
  269. + ileft -= todo;
  270. + sgileft -= todo;
  271. + }
  272. + while (todo > 0) {
  273. + writel_relaxed(*src32++, ss->base + SS_RXFIFO);
  274. + todo--;
  275. + }
  276. + if (in_sg != NULL && sgileft == 0 && ileft > 0) {
  277. + kunmap(sg_page(in_sg));
  278. + in_sg = sg_next(in_sg);
  279. + while (in_sg != NULL && in_sg->length == 0)
  280. + in_sg = sg_next(in_sg);
  281. + if (in_sg != NULL && ileft > 0) {
  282. + src_addr = kmap(sg_page(in_sg)) + in_sg->offset;
  283. + if (src_addr == NULL) {
  284. + dev_err(ss->dev, "ERROR: KMAP for src SG\n");
  285. + mutex_unlock(&ss->lock);
  286. + return -EINVAL;
  287. + }
  288. + src32 = src_addr;
  289. + sgileft = in_sg->length / 4;
  290. + }
  291. + }
  292. + /* do not test oleft since when oleft == 0 we have finished */
  293. + todo = min3(tx_cnt, oleft, sgoleft);
  294. + if (todo > 0) {
  295. + oleft -= todo;
  296. + sgoleft -= todo;
  297. + }
  298. + while (todo > 0) {
  299. + *dst32++ = readl_relaxed(ss->base + SS_TXFIFO);
  300. + todo--;
  301. + }
  302. + if (out_sg != NULL && sgoleft == 0 && oleft >= 0) {
  303. + kunmap(sg_page(out_sg));
  304. + out_sg = sg_next(out_sg);
  305. + while (out_sg != NULL && out_sg->length == 0)
  306. + out_sg = sg_next(out_sg);
  307. + if (out_sg != NULL && oleft > 0) {
  308. + dst_addr = kmap(sg_page(out_sg)) +
  309. + out_sg->offset;
  310. + if (dst_addr == NULL) {
  311. + dev_err(ss->dev, "KMAP error\n");
  312. + mutex_unlock(&ss->lock);
  313. + return -EINVAL;
  314. + }
  315. + dst32 = dst_addr;
  316. + sgoleft = out_sg->length / 4;
  317. + }
  318. + }
  319. + } while (oleft > 0);
  320. +
  321. + writel_relaxed(0, ss->base + SS_CTL);
  322. + mutex_unlock(&ss->lock);
  323. + return 0;
  324. +}
  325. +
  326. +/*
  327. + * Pure CPU way of doing DES/3DES with SS
  328. + * Since DES and 3DES SGs could be smaller than 4 bytes, I use sg_copy_to_buffer
  329. + * for "linearize" them.
  330. + * The problem with that is that I alloc (2 x areq->nbytes) for buf_in/buf_out
  331. + * TODO: change this system, I need to support other mode than CBC where len
  332. + * is not a multiple of 4 and the hack of linearize use too much memory
  333. + * SGsrc -> buf_in -> SS -> buf_out -> SGdst
  334. + */
  335. +int sunxi_ss_des_poll(struct ablkcipher_request *areq, u32 mode)
  336. +{
  337. + u32 value, spaces;
  338. + size_t nb_in_sg_tx, nb_in_sg_rx;
  339. + size_t ir, it;
  340. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  341. + struct sunxi_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  342. + unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
  343. + u32 tx_cnt = 0;
  344. + u32 rx_cnt = 0;
  345. + u32 v;
  346. + int i;
  347. + int no_chunk = 1;
  348. + struct scatterlist *in_sg = areq->src;
  349. + struct scatterlist *out_sg = areq->dst;
  350. +
  351. + /*
  352. + * if we have only SGs with size multiple of 4,
  353. + * we can use the SS AES function
  354. + */
  355. + while (in_sg != NULL && no_chunk == 1) {
  356. + if ((in_sg->length % 4) != 0)
  357. + no_chunk = 0;
  358. + in_sg = sg_next(in_sg);
  359. + }
  360. + while (out_sg != NULL && no_chunk == 1) {
  361. + if ((out_sg->length % 4) != 0)
  362. + no_chunk = 0;
  363. + out_sg = sg_next(out_sg);
  364. + }
  365. +
  366. + if (no_chunk == 1)
  367. + return sunxi_ss_aes_poll(areq, mode);
  368. +
  369. + in_sg = areq->src;
  370. + out_sg = areq->dst;
  371. +
  372. + nb_in_sg_rx = sg_nents(in_sg);
  373. + nb_in_sg_tx = sg_nents(out_sg);
  374. +
  375. + /*
  376. + * buf_in and buf_out are allocated only one time
  377. + * then we keep the buffer until driver end
  378. + * the allocation can only grow more
  379. + * we do not reduce it for simplification
  380. + */
  381. + mutex_lock(&ss->bufin_lock);
  382. + if (ss->buf_in == NULL) {
  383. + ss->buf_in = kmalloc(areq->nbytes, GFP_KERNEL);
  384. + ss->buf_in_size = areq->nbytes;
  385. + } else {
  386. + if (areq->nbytes > ss->buf_in_size) {
  387. + kfree(ss->buf_in);
  388. + ss->buf_in = kmalloc(areq->nbytes, GFP_KERNEL);
  389. + ss->buf_in_size = areq->nbytes;
  390. + }
  391. + }
  392. + if (ss->buf_in == NULL) {
  393. + ss->buf_in_size = 0;
  394. + mutex_unlock(&ss->bufin_lock);
  395. + dev_err(ss->dev, "Unable to allocate pages.\n");
  396. + return -ENOMEM;
  397. + }
  398. + mutex_lock(&ss->bufout_lock);
  399. + if (ss->buf_out == NULL) {
  400. + ss->buf_out = kmalloc(areq->nbytes, GFP_KERNEL);
  401. + if (ss->buf_out == NULL) {
  402. + ss->buf_out_size = 0;
  403. + mutex_unlock(&ss->bufin_lock);
  404. + mutex_unlock(&ss->bufout_lock);
  405. + dev_err(ss->dev, "Unable to allocate pages.\n");
  406. + return -ENOMEM;
  407. + }
  408. + ss->buf_out_size = areq->nbytes;
  409. + } else {
  410. + if (areq->nbytes > ss->buf_out_size) {
  411. + kfree(ss->buf_out);
  412. + ss->buf_out = kmalloc(areq->nbytes, GFP_KERNEL);
  413. + if (ss->buf_out == NULL) {
  414. + ss->buf_out_size = 0;
  415. + mutex_unlock(&ss->bufin_lock);
  416. + mutex_unlock(&ss->bufout_lock);
  417. + dev_err(ss->dev, "Unable to allocate pages.\n");
  418. + return -ENOMEM;
  419. + }
  420. + ss->buf_out_size = areq->nbytes;
  421. + }
  422. + }
  423. +
  424. + sg_copy_to_buffer(areq->src, nb_in_sg_rx, ss->buf_in, areq->nbytes);
  425. +
  426. + ir = 0;
  427. + it = 0;
  428. + mutex_lock(&ss->lock);
  429. +
  430. + for (i = 0; i < op->keylen; i += 4)
  431. + writel(*(op->key + i/4), ss->base + SS_KEY0 + i);
  432. + if (areq->info != NULL) {
  433. + for (i = 0; i < 4 && i < ivsize / 4; i++) {
  434. + v = *(u32 *)(areq->info + i * 4);
  435. + writel(v, ss->base + SS_IV0 + i * 4);
  436. + }
  437. + }
  438. + writel(mode, ss->base + SS_CTL);
  439. +
  440. + do {
  441. + if (rx_cnt == 0 || tx_cnt == 0) {
  442. + spaces = readl(ss->base + SS_FCSR);
  443. + rx_cnt = SS_RXFIFO_SPACES(spaces);
  444. + tx_cnt = SS_TXFIFO_SPACES(spaces);
  445. + }
  446. + if (rx_cnt > 0 && ir < areq->nbytes) {
  447. + do {
  448. + value = *(u32 *)(ss->buf_in + ir);
  449. + writel(value, ss->base + SS_RXFIFO);
  450. + ir += 4;
  451. + rx_cnt--;
  452. + } while (rx_cnt > 0 && ir < areq->nbytes);
  453. + }
  454. + if (tx_cnt > 0 && it < areq->nbytes) {
  455. + do {
  456. + value = readl(ss->base + SS_TXFIFO);
  457. + *(u32 *)(ss->buf_out + it) = value;
  458. + it += 4;
  459. + tx_cnt--;
  460. + } while (tx_cnt > 0 && it < areq->nbytes);
  461. + }
  462. + if (ir == areq->nbytes) {
  463. + mutex_unlock(&ss->bufin_lock);
  464. + ir++;
  465. + }
  466. + } while (it < areq->nbytes);
  467. +
  468. + writel(0, ss->base + SS_CTL);
  469. + mutex_unlock(&ss->lock);
  470. +
  471. + /*
  472. + * a simple optimization, since we dont need the hardware for this copy
  473. + * we release the lock and do the copy. With that we gain 5/10% perf
  474. + */
  475. + sg_copy_from_buffer(areq->dst, nb_in_sg_tx, ss->buf_out, areq->nbytes);
  476. +
  477. + mutex_unlock(&ss->bufout_lock);
  478. + return 0;
  479. +}
  480. +
  481. +/* check and set the AES key, prepare the mode to be used */
  482. +int sunxi_ss_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  483. + unsigned int keylen)
  484. +{
  485. + struct sunxi_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  486. +
  487. + switch (keylen) {
  488. + case 128 / 8:
  489. + op->keymode = SS_AES_128BITS;
  490. + break;
  491. + case 192 / 8:
  492. + op->keymode = SS_AES_192BITS;
  493. + break;
  494. + case 256 / 8:
  495. + op->keymode = SS_AES_256BITS;
  496. + break;
  497. + default:
  498. + dev_err(ss->dev, "ERROR: Invalid keylen %u\n", keylen);
  499. + crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  500. + return -EINVAL;
  501. + }
  502. + op->keylen = keylen;
  503. + memcpy(op->key, key, keylen);
  504. + return 0;
  505. +}
  506. +
  507. +/* check and set the DES key, prepare the mode to be used */
  508. +int sunxi_ss_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  509. + unsigned int keylen)
  510. +{
  511. + struct sunxi_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  512. +
  513. + if (keylen != DES_KEY_SIZE) {
  514. + dev_err(ss->dev, "Invalid keylen %u\n", keylen);
  515. + crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  516. + return -EINVAL;
  517. + }
  518. + op->keylen = keylen;
  519. + memcpy(op->key, key, keylen);
  520. + return 0;
  521. +}
  522. +
  523. +/* check and set the 3DES key, prepare the mode to be used */
  524. +int sunxi_ss_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  525. + unsigned int keylen)
  526. +{
  527. + struct sunxi_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  528. +
  529. + if (keylen != 3 * DES_KEY_SIZE) {
  530. + dev_err(ss->dev, "Invalid keylen %u\n", keylen);
  531. + crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  532. + return -EINVAL;
  533. + }
  534. + op->keylen = keylen;
  535. + memcpy(op->key, key, keylen);
  536. + return 0;
  537. +}
  538. diff --git a/drivers/crypto/sunxi-ss/sunxi-ss-core.c b/drivers/crypto/sunxi-ss/sunxi-ss-core.c
  539. new file mode 100644
  540. index 0000000..e66d7e2
  541. --- /dev/null
  542. +++ b/drivers/crypto/sunxi-ss/sunxi-ss-core.c
  543. @@ -0,0 +1,318 @@
  544. +/*
  545. + * sunxi-ss-core.c - hardware cryptographic accelerator for Allwinner A20 SoC
  546. + *
  547. + * Copyright (C) 2013-2014 Corentin LABBE <clabbe.montjoie@gmail.com>
  548. + *
  549. + * Core file which registers crypto algorithms supported by the SS.
  550. + *
  551. + * You could find a link for the datasheet in Documentation/arm/sunxi/README
  552. + *
  553. + * This program is free software; you can redistribute it and/or modify
  554. + * it under the terms of the GNU General Public License as published by
  555. + * the Free Software Foundation; either version 2 of the License, or
  556. + * (at your option) any later version.
  557. + */
  558. +#include <linux/clk.h>
  559. +#include <linux/crypto.h>
  560. +#include <linux/io.h>
  561. +#include <linux/module.h>
  562. +#include <linux/of.h>
  563. +#include <linux/platform_device.h>
  564. +#include <crypto/scatterwalk.h>
  565. +#include <linux/scatterlist.h>
  566. +#include <linux/interrupt.h>
  567. +#include <linux/delay.h>
  568. +
  569. +#include "sunxi-ss.h"
  570. +
  571. +struct sunxi_ss_ctx *ss;
  572. +
  573. +/*
  574. + * General notes for whole driver:
  575. + *
  576. + * After each request the device must be disabled with a write of 0 in SS_CTL
  577. + *
  578. + * For performance reason, we use writel_relaxed/read_relaxed for all
  579. + * operations on RX and TX FIFO and also SS_FCSR.
  580. + * Excepts for the last write on TX FIFO.
  581. + * For all other registers, we use writel/readl.
  582. + * See http://permalink.gmane.org/gmane.linux.ports.arm.kernel/117644
  583. + * and http://permalink.gmane.org/gmane.linux.ports.arm.kernel/117640
  584. + */
  585. +
  586. +static struct ahash_alg sunxi_md5_alg = {
  587. + .init = sunxi_hash_init,
  588. + .update = sunxi_hash_update,
  589. + .final = sunxi_hash_final,
  590. + .finup = sunxi_hash_finup,
  591. + .digest = sunxi_hash_digest,
  592. + .halg = {
  593. + .digestsize = MD5_DIGEST_SIZE,
  594. + .base = {
  595. + .cra_name = "md5",
  596. + .cra_driver_name = "md5-sunxi-ss",
  597. + .cra_priority = 300,
  598. + .cra_alignmask = 3,
  599. + .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
  600. + .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  601. + .cra_ctxsize = sizeof(struct sunxi_req_ctx),
  602. + .cra_module = THIS_MODULE,
  603. + .cra_type = &crypto_ahash_type,
  604. + .cra_init = sunxi_hash_crainit
  605. + }
  606. + }
  607. +};
  608. +
  609. +static struct ahash_alg sunxi_sha1_alg = {
  610. + .init = sunxi_hash_init,
  611. + .update = sunxi_hash_update,
  612. + .final = sunxi_hash_final,
  613. + .finup = sunxi_hash_finup,
  614. + .digest = sunxi_hash_digest,
  615. + .halg = {
  616. + .digestsize = SHA1_DIGEST_SIZE,
  617. + .base = {
  618. + .cra_name = "sha1",
  619. + .cra_driver_name = "sha1-sunxi-ss",
  620. + .cra_priority = 300,
  621. + .cra_alignmask = 3,
  622. + .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
  623. + .cra_blocksize = SHA1_BLOCK_SIZE,
  624. + .cra_ctxsize = sizeof(struct sunxi_req_ctx),
  625. + .cra_module = THIS_MODULE,
  626. + .cra_type = &crypto_ahash_type,
  627. + .cra_init = sunxi_hash_crainit
  628. + }
  629. + }
  630. +};
  631. +
  632. +static struct crypto_alg sunxi_cipher_algs[] = {
  633. +{
  634. + .cra_name = "cbc(aes)",
  635. + .cra_driver_name = "cbc-aes-sunxi-ss",
  636. + .cra_priority = 300,
  637. + .cra_blocksize = AES_BLOCK_SIZE,
  638. + .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  639. + .cra_ctxsize = sizeof(struct sunxi_tfm_ctx),
  640. + .cra_module = THIS_MODULE,
  641. + .cra_alignmask = 3,
  642. + .cra_type = &crypto_ablkcipher_type,
  643. + .cra_init = sunxi_ss_cipher_init,
  644. + .cra_u = {
  645. + .ablkcipher = {
  646. + .min_keysize = AES_MIN_KEY_SIZE,
  647. + .max_keysize = AES_MAX_KEY_SIZE,
  648. + .ivsize = AES_BLOCK_SIZE,
  649. + .setkey = sunxi_ss_aes_setkey,
  650. + .encrypt = sunxi_ss_cipher_encrypt,
  651. + .decrypt = sunxi_ss_cipher_decrypt,
  652. + }
  653. + }
  654. +}, {
  655. + .cra_name = "cbc(des)",
  656. + .cra_driver_name = "cbc-des-sunxi-ss",
  657. + .cra_priority = 300,
  658. + .cra_blocksize = DES_BLOCK_SIZE,
  659. + .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  660. + .cra_ctxsize = sizeof(struct sunxi_req_ctx),
  661. + .cra_module = THIS_MODULE,
  662. + .cra_alignmask = 3,
  663. + .cra_type = &crypto_ablkcipher_type,
  664. + .cra_init = sunxi_ss_cipher_init,
  665. + .cra_u.ablkcipher = {
  666. + .min_keysize = DES_KEY_SIZE,
  667. + .max_keysize = DES_KEY_SIZE,
  668. + .ivsize = DES_BLOCK_SIZE,
  669. + .setkey = sunxi_ss_des_setkey,
  670. + .encrypt = sunxi_ss_cipher_encrypt,
  671. + .decrypt = sunxi_ss_cipher_decrypt,
  672. + }
  673. +}, {
  674. + .cra_name = "cbc(des3_ede)",
  675. + .cra_driver_name = "cbc-des3-sunxi-ss",
  676. + .cra_priority = 300,
  677. + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  678. + .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  679. + .cra_ctxsize = sizeof(struct sunxi_req_ctx),
  680. + .cra_module = THIS_MODULE,
  681. + .cra_alignmask = 3,
  682. + .cra_type = &crypto_ablkcipher_type,
  683. + .cra_init = sunxi_ss_cipher_init,
  684. + .cra_u.ablkcipher = {
  685. + .min_keysize = DES3_EDE_KEY_SIZE,
  686. + .max_keysize = DES3_EDE_KEY_SIZE,
  687. + .ivsize = DES3_EDE_BLOCK_SIZE,
  688. + .setkey = sunxi_ss_des3_setkey,
  689. + .encrypt = sunxi_ss_cipher_encrypt,
  690. + .decrypt = sunxi_ss_cipher_decrypt,
  691. + }
  692. +}
  693. +};
  694. +
  695. +static int sunxi_ss_probe(struct platform_device *pdev)
  696. +{
  697. + struct resource *res;
  698. + u32 v;
  699. + int err;
  700. + unsigned long cr;
  701. + const unsigned long cr_ahb = 24 * 1000 * 1000;
  702. + const unsigned long cr_mod = 150 * 1000 * 1000;
  703. +
  704. + if (!pdev->dev.of_node)
  705. + return -ENODEV;
  706. +
  707. + ss = devm_kzalloc(&pdev->dev, sizeof(*ss), GFP_KERNEL);
  708. + if (ss == NULL)
  709. + return -ENOMEM;
  710. +
  711. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  712. + ss->base = devm_ioremap_resource(&pdev->dev, res);
  713. + if (IS_ERR(ss->base)) {
  714. + dev_err(&pdev->dev, "Cannot request MMIO\n");
  715. + return PTR_ERR(ss->base);
  716. + }
  717. +
  718. + ss->ssclk = devm_clk_get(&pdev->dev, "mod");
  719. + if (IS_ERR(ss->ssclk)) {
  720. + err = PTR_ERR(ss->ssclk);
  721. + dev_err(&pdev->dev, "Cannot get SS clock err=%d\n", err);
  722. + return err;
  723. + }
  724. + dev_dbg(&pdev->dev, "clock ss acquired\n");
  725. +
  726. + ss->busclk = devm_clk_get(&pdev->dev, "ahb");
  727. + if (IS_ERR(ss->busclk)) {
  728. + err = PTR_ERR(ss->busclk);
  729. + dev_err(&pdev->dev, "Cannot get AHB SS clock err=%d\n", err);
  730. + return err;
  731. + }
  732. + dev_dbg(&pdev->dev, "clock ahb_ss acquired\n");
  733. +
  734. + /* Enable both clocks */
  735. + err = clk_prepare_enable(ss->busclk);
  736. + if (err != 0) {
  737. + dev_err(&pdev->dev, "Cannot prepare_enable busclk\n");
  738. + return err;
  739. + }
  740. + err = clk_prepare_enable(ss->ssclk);
  741. + if (err != 0) {
  742. + dev_err(&pdev->dev, "Cannot prepare_enable ssclk\n");
  743. + clk_disable_unprepare(ss->busclk);
  744. + return err;
  745. + }
  746. +
  747. + /*
  748. + * Check that clock have the correct rates gived in the datasheet
  749. + * Try to set the clock to the maximum allowed
  750. + */
  751. + err = clk_set_rate(ss->ssclk, cr_mod);
  752. + if (err != 0) {
  753. + dev_err(&pdev->dev, "Cannot set clock rate to ssclk\n");
  754. + clk_disable_unprepare(ss->ssclk);
  755. + clk_disable_unprepare(ss->busclk);
  756. + return err;
  757. + }
  758. +
  759. + cr = clk_get_rate(ss->busclk);
  760. + if (cr >= cr_ahb)
  761. + dev_dbg(&pdev->dev, "Clock bus %lu (%lu MHz) (must be >= %lu)\n",
  762. + cr, cr / 1000000, cr_ahb);
  763. + else
  764. + dev_warn(&pdev->dev, "Clock bus %lu (%lu MHz) (must be >= %lu)\n",
  765. + cr, cr / 1000000, cr_ahb);
  766. +
  767. + cr = clk_get_rate(ss->ssclk);
  768. + if (cr <= cr_mod)
  769. + if (cr < cr_mod)
  770. + dev_info(&pdev->dev, "Clock ss %lu (%lu MHz) (must be <= %lu)\n",
  771. + cr, cr / 1000000, cr_mod);
  772. + else
  773. + dev_dbg(&pdev->dev, "Clock ss %lu (%lu MHz) (must be <= %lu)\n",
  774. + cr, cr / 1000000, cr_mod);
  775. + else
  776. + dev_warn(&pdev->dev, "Clock ss is at %lu (%lu MHz) (must be <= %lu)\n",
  777. + cr, cr / 1000000, cr_mod);
  778. +
  779. + /*
  780. + * Datasheet named it "Die Bonding ID"
  781. + * I expect to be a sort of Security System Revision number.
  782. + * Since the A80 seems to have an other version of SS
  783. + * this info could be useful
  784. + */
  785. + writel(SS_ENABLED, ss->base + SS_CTL);
  786. + v = readl(ss->base + SS_CTL);
  787. + v >>= 16;
  788. + v &= 0x07;
  789. + dev_info(&pdev->dev, "Die ID %d\n", v);
  790. + writel(0, ss->base + SS_CTL);
  791. +
  792. + ss->dev = &pdev->dev;
  793. +
  794. + mutex_init(&ss->lock);
  795. + mutex_init(&ss->bufin_lock);
  796. + mutex_init(&ss->bufout_lock);
  797. +
  798. + err = crypto_register_ahash(&sunxi_md5_alg);
  799. + if (err)
  800. + goto error_md5;
  801. + err = crypto_register_ahash(&sunxi_sha1_alg);
  802. + if (err)
  803. + goto error_sha1;
  804. + err = crypto_register_algs(sunxi_cipher_algs,
  805. + ARRAY_SIZE(sunxi_cipher_algs));
  806. + if (err)
  807. + goto error_ciphers;
  808. +
  809. + return 0;
  810. +error_ciphers:
  811. + crypto_unregister_ahash(&sunxi_sha1_alg);
  812. +error_sha1:
  813. + crypto_unregister_ahash(&sunxi_md5_alg);
  814. +error_md5:
  815. + clk_disable_unprepare(ss->ssclk);
  816. + clk_disable_unprepare(ss->busclk);
  817. + return err;
  818. +}
  819. +
  820. +static int __exit sunxi_ss_remove(struct platform_device *pdev)
  821. +{
  822. + if (!pdev->dev.of_node)
  823. + return 0;
  824. +
  825. + crypto_unregister_ahash(&sunxi_md5_alg);
  826. + crypto_unregister_ahash(&sunxi_sha1_alg);
  827. + crypto_unregister_algs(sunxi_cipher_algs,
  828. + ARRAY_SIZE(sunxi_cipher_algs));
  829. +
  830. + if (ss->buf_in != NULL)
  831. + kfree(ss->buf_in);
  832. + if (ss->buf_out != NULL)
  833. + kfree(ss->buf_out);
  834. +
  835. + writel(0, ss->base + SS_CTL);
  836. + clk_disable_unprepare(ss->busclk);
  837. + clk_disable_unprepare(ss->ssclk);
  838. + return 0;
  839. +}
  840. +
  841. +static const struct of_device_id a20ss_crypto_of_match_table[] = {
  842. + { .compatible = "allwinner,sun7i-a20-crypto" },
  843. + {}
  844. +};
  845. +MODULE_DEVICE_TABLE(of, a20ss_crypto_of_match_table);
  846. +
  847. +static struct platform_driver sunxi_ss_driver = {
  848. + .probe = sunxi_ss_probe,
  849. + .remove = __exit_p(sunxi_ss_remove),
  850. + .driver = {
  851. + .owner = THIS_MODULE,
  852. + .name = "sunxi-ss",
  853. + .of_match_table = a20ss_crypto_of_match_table,
  854. + },
  855. +};
  856. +
  857. +module_platform_driver(sunxi_ss_driver);
  858. +
  859. +MODULE_DESCRIPTION("Allwinner Security System cryptographic accelerator");
  860. +MODULE_LICENSE("GPL");
  861. +MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
  862. diff --git a/drivers/crypto/sunxi-ss/sunxi-ss-hash.c b/drivers/crypto/sunxi-ss/sunxi-ss-hash.c
  863. new file mode 100644
  864. index 0000000..ec8758f
  865. --- /dev/null
  866. +++ b/drivers/crypto/sunxi-ss/sunxi-ss-hash.c
  867. @@ -0,0 +1,445 @@
  868. +/*
  869. + * sunxi-ss-hash.c - hardware cryptographic accelerator for Allwinner A20 SoC
  870. + *
  871. + * Copyright (C) 2013-2014 Corentin LABBE <clabbe.montjoie@gmail.com>
  872. + *
  873. + * This file add support for MD5 and SHA1.
  874. + *
  875. + * You could find the datasheet in Documentation/arm/sunxi/README
  876. + *
  877. + * This program is free software; you can redistribute it and/or modify
  878. + * it under the terms of the GNU General Public License as published by
  879. + * the Free Software Foundation; either version 2 of the License, or
  880. + * (at your option) any later version.
  881. + */
  882. +#include "sunxi-ss.h"
  883. +
  884. +/* This is a totaly arbitrary value */
  885. +#define SS_TIMEOUT 100
  886. +
  887. +extern struct sunxi_ss_ctx *ss;
  888. +
  889. +int sunxi_hash_crainit(struct crypto_tfm *tfm)
  890. +{
  891. + crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  892. + sizeof(struct sunxi_req_ctx));
  893. + return 0;
  894. +}
  895. +
  896. +/* sunxi_hash_init: initialize request context */
  897. +int sunxi_hash_init(struct ahash_request *areq)
  898. +{
  899. + const char *hash_type;
  900. + struct sunxi_req_ctx *op = ahash_request_ctx(areq);
  901. +
  902. + memset(op, 0, sizeof(struct sunxi_req_ctx));
  903. +
  904. + hash_type = crypto_tfm_alg_name(areq->base.tfm);
  905. +
  906. + if (strcmp(hash_type, "sha1") == 0)
  907. + op->mode = SS_OP_SHA1;
  908. + if (strcmp(hash_type, "md5") == 0)
  909. + op->mode = SS_OP_MD5;
  910. + if (op->mode == 0)
  911. + return -EINVAL;
  912. +
  913. + return 0;
  914. +}
  915. +
  916. +static u32 rx_cnt;
  917. +
  918. +inline void ss_writer(const u32 v)
  919. +{
  920. + u32 spaces;
  921. +
  922. + writel(v, ss->base + SS_RXFIFO);
  923. + rx_cnt--;
  924. + while (rx_cnt == 0) {
  925. + spaces = readl_relaxed(ss->base + SS_FCSR);
  926. + rx_cnt = SS_RXFIFO_SPACES(spaces);
  927. + }
  928. +}
  929. +
  930. +inline void ss_writer_relaxed(const u32 v)
  931. +{
  932. + u32 spaces;
  933. +
  934. + writel_relaxed(v, ss->base + SS_RXFIFO);
  935. + rx_cnt--;
  936. + while (rx_cnt == 0) {
  937. + spaces = readl_relaxed(ss->base + SS_FCSR);
  938. + rx_cnt = SS_RXFIFO_SPACES(spaces);
  939. + }
  940. +}
  941. +
  942. +/*
  943. + * sunxi_hash_update: update hash engine
  944. + *
  945. + * Could be used for both SHA1 and MD5
  946. + * Write data by step of 32bits and put then in the SS.
  947. + *
  948. + * Since we cannot leave partial data and hash state in the engine,
  949. + * we need to get the hash state at the end of this function.
  950. + * After some work, I have found that we can get the hash state every 64o
  951. + *
  952. + * So the first work is to get the number of bytes to write to SS modulo 64
  953. + * The extra bytes will go to two different destination:
  954. + * op->wait for full 32bits word
  955. + * op->wb (waiting bytes) for partial 32 bits word
  956. + * So we can have up to (64/4)-1 op->wait words and 0/1/2/3 bytes in wb
  957. + *
  958. + * So at the begin of update()
  959. + * if op->nwait * 4 + areq->nbytes < 64
  960. + * => all data writed to wait buffers and end=0
  961. + * if not write all nwait to the device and position end to complete to 64o
  962. + *
  963. + * example 1:
  964. + * update1 60o => nwait=15
  965. + * update2 60o => need one more word to have 64o
  966. + * end=4
  967. + * so write all data in op->wait and one word of SGs
  968. + * write remaining data in op->wait
  969. + * final state op->nwait=14
  970. + */
  971. +int sunxi_hash_update(struct ahash_request *areq)
  972. +{
  973. + u32 v, ivmode = 0;
  974. + unsigned int i = 0;
  975. + /*
  976. + * i is the total bytes read from SGs, to be compared to areq->nbytes
  977. + * i is important because we cannot rely on SG length since the sum of
  978. + * SG->length could be greater than areq->nbytes
  979. + */
  980. +
  981. + struct sunxi_req_ctx *op = ahash_request_ctx(areq);
  982. + struct scatterlist *in_sg;
  983. + unsigned int in_i = 0; /* advancement in the current SG */
  984. + u64 end;
  985. + /*
  986. + * end is the position when we need to stop writing to the device,
  987. + * to be compared to i
  988. + */
  989. + int in_r;
  990. + void *src_addr;
  991. +
  992. + dev_dbg(ss->dev, "%s %s bc=%llu len=%u mode=%x bw=%u ww=%u",
  993. + __func__, crypto_tfm_alg_name(areq->base.tfm),
  994. + op->byte_count, areq->nbytes, op->mode,
  995. + op->nbw, op->nwait);
  996. +
  997. + if (areq->nbytes == 0)
  998. + return 0;
  999. +
  1000. + end = ((areq->nbytes + op->nwait * 4 + op->nbw) / 64) * 64
  1001. + - op->nbw - op->nwait * 4;
  1002. +
  1003. + if (end > areq->nbytes || areq->nbytes - end > 63) {
  1004. + dev_err(ss->dev, "ERROR: Bound error %llu %u\n",
  1005. + end, areq->nbytes);
  1006. + return -EINVAL;
  1007. + }
  1008. +
  1009. + if (op->nwait > 0 && end > 0) {
  1010. + /* a precedent update was done */
  1011. + for (i = 0; i < op->nwait; i++) {
  1012. + ss_writer(op->wait[i]);
  1013. + op->byte_count += 4;
  1014. + }
  1015. + op->nwait = 0;
  1016. + }
  1017. +
  1018. + mutex_lock(&ss->lock);
  1019. + /*
  1020. + * if some data have been processed before,
  1021. + * we need to restore the partial hash state
  1022. + */
  1023. + if (op->byte_count > 0) {
  1024. + ivmode = SS_IV_ARBITRARY;
  1025. + for (i = 0; i < 5; i++)
  1026. + writel(op->hash[i], ss->base + SS_IV0 + i * 4);
  1027. + }
  1028. + /* Enable the device */
  1029. + writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL);
  1030. +
  1031. + rx_cnt = 0;
  1032. + i = 0;
  1033. +
  1034. + in_sg = areq->src;
  1035. + src_addr = kmap(sg_page(in_sg)) + in_sg->offset;
  1036. + if (src_addr == NULL) {
  1037. + mutex_unlock(&ss->lock);
  1038. + dev_err(ss->dev, "ERROR: Cannot kmap source buffer\n");
  1039. + return -EFAULT;
  1040. + }
  1041. + do {
  1042. + /*
  1043. + * step 1, if some bytes remains from last SG,
  1044. + * try to complete them to 4 and send that word
  1045. + */
  1046. + if (op->nbw > 0) {
  1047. + while (op->nbw < 4 && i < areq->nbytes &&
  1048. + in_i < in_sg->length) {
  1049. + op->wb |= (*(u8 *)(src_addr + in_i))
  1050. + << (8 * op->nbw);
  1051. + dev_dbg(ss->dev, "%s Complete w=%d wb=%x\n",
  1052. + __func__, op->nbw, op->wb);
  1053. + i++;
  1054. + in_i++;
  1055. + op->nbw++;
  1056. + }
  1057. + if (op->nbw == 4) {
  1058. + if (i <= end) {
  1059. + ss_writer(op->wb);
  1060. + op->byte_count += 4;
  1061. + } else {
  1062. + op->wait[op->nwait] = op->wb;
  1063. + op->nwait++;
  1064. + dev_dbg(ss->dev, "%s Keep %u bytes after %llu\n",
  1065. + __func__, op->nwait, end);
  1066. + }
  1067. + op->nbw = 0;
  1068. + op->wb = 0;
  1069. + }
  1070. + }
  1071. + /* step 2, main loop, read data 4bytes at a time */
  1072. + while (i < areq->nbytes && in_i < in_sg->length) {
  1073. + /* how many bytes we can read, (we need 4) */
  1074. + in_r = min(in_sg->length - in_i, areq->nbytes - i);
  1075. + if (in_r < 4) {
  1076. + /* Not enough data to write to the device */
  1077. + op->wb = 0;
  1078. + while (in_r > 0) {
  1079. + op->wb |= (*(u8 *)(src_addr + in_i))
  1080. + << (8 * op->nbw);
  1081. + dev_dbg(ss->dev, "%s ending bw=%d wb=%x\n",
  1082. + __func__, op->nbw, op->wb);
  1083. + in_r--;
  1084. + i++;
  1085. + in_i++;
  1086. + op->nbw++;
  1087. + }
  1088. + goto nextsg;
  1089. + }
  1090. + v = *(u32 *)(src_addr + in_i);
  1091. + if (i < end) {
  1092. + /* last write must be done without relaxed */
  1093. + if (i + 4 >= end)
  1094. + ss_writer(v);
  1095. + else
  1096. + ss_writer_relaxed(v);
  1097. + i += 4;
  1098. + op->byte_count += 4;
  1099. + in_i += 4;
  1100. + } else {
  1101. + op->wait[op->nwait] = v;
  1102. + i += 4;
  1103. + in_i += 4;
  1104. + op->nwait++;
  1105. + dev_dbg(ss->dev, "%s Keep word ww=%u after %llu\n",
  1106. + __func__, op->nwait, end);
  1107. + if (op->nwait > 15) {
  1108. + dev_err(ss->dev, "FATAL: Cannot enqueue more, bug?\n");
  1109. + writel(0, ss->base + SS_CTL);
  1110. + mutex_unlock(&ss->lock);
  1111. + return -EIO;
  1112. + }
  1113. + }
  1114. + }
  1115. +nextsg:
  1116. + /* Nothing more to read in this SG */
  1117. + if (in_i == in_sg->length) {
  1118. + kunmap(sg_page(in_sg));
  1119. + do {
  1120. + in_sg = sg_next(in_sg);
  1121. + } while (in_sg != NULL && in_sg->length == 0);
  1122. + in_i = 0;
  1123. + if (in_sg != NULL) {
  1124. + src_addr = kmap(sg_page(in_sg)) + in_sg->offset;
  1125. + if (src_addr == NULL) {
  1126. + mutex_unlock(&ss->lock);
  1127. + dev_err(ss->dev, "ERROR: Cannot kmap source buffer\n");
  1128. + return -EFAULT;
  1129. + }
  1130. + }
  1131. + }
  1132. + } while (in_sg != NULL && i < areq->nbytes);
  1133. +
  1134. + /* ask the device to finish the hashing */
  1135. + writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
  1136. + i = 0;
  1137. + do {
  1138. + v = readl(ss->base + SS_CTL);
  1139. + i++;
  1140. + } while (i < SS_TIMEOUT && (v & SS_DATA_END) > 0);
  1141. + if (i >= SS_TIMEOUT) {
  1142. + dev_err(ss->dev, "ERROR: %s hash end timeout after %d loop, CTL=%x\n",
  1143. + __func__, i, v);
  1144. + writel(0, ss->base + SS_CTL);
  1145. + mutex_unlock(&ss->lock);
  1146. + return -EIO;
  1147. + }
  1148. +
  1149. + /* get the partial hash */
  1150. + if (op->mode == SS_OP_SHA1) {
  1151. + for (i = 0; i < 5; i++)
  1152. + op->hash[i] = readl(ss->base + SS_MD0 + i * 4);
  1153. + } else {
  1154. + for (i = 0; i < 4; i++)
  1155. + op->hash[i] = readl(ss->base + SS_MD0 + i * 4);
  1156. + }
  1157. +
  1158. + writel(0, ss->base + SS_CTL);
  1159. + mutex_unlock(&ss->lock);
  1160. + return 0;
  1161. +}
  1162. +
  1163. +/*
  1164. + * sunxi_hash_final: finalize hashing operation
  1165. + *
  1166. + * If we have some remaining bytes, we write them.
  1167. + * Then ask the SS for finalizing the hashing operation
  1168. + */
  1169. +int sunxi_hash_final(struct ahash_request *areq)
  1170. +{
  1171. + u32 v, ivmode = 0;
  1172. + unsigned int i;
  1173. + int zeros;
  1174. + unsigned int index, padlen;
  1175. + __be64 bits;
  1176. + struct sunxi_req_ctx *op = ahash_request_ctx(areq);
  1177. +
  1178. + dev_dbg(ss->dev, "%s byte=%llu len=%u mode=%x bw=%u %x h=%x ww=%u",
  1179. + __func__, op->byte_count, areq->nbytes, op->mode,
  1180. + op->nbw, op->wb, op->hash[0], op->nwait);
  1181. +
  1182. + mutex_lock(&ss->lock);
  1183. + rx_cnt = 0;
  1184. +
  1185. + /*
  1186. + * if we have already writed something,
  1187. + * restore the partial hash state
  1188. + */
  1189. + if (op->byte_count > 0) {
  1190. + ivmode = SS_IV_ARBITRARY;
  1191. + for (i = 0; i < 5; i++)
  1192. + writel(op->hash[i], ss->base + SS_IV0 + i * 4);
  1193. + }
  1194. + writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL);
  1195. +
  1196. + /* write the remaining words of the wait buffer */
  1197. + if (op->nwait > 0) {
  1198. + for (i = 0; i < op->nwait; i++) {
  1199. + v = op->wait[i];
  1200. + ss_writer(v);
  1201. + op->byte_count += 4;
  1202. + dev_dbg(ss->dev, "%s write %llu i=%u %x\n",
  1203. + __func__, op->byte_count, i, v);
  1204. + }
  1205. + op->nwait = 0;
  1206. + }
  1207. +
  1208. + /* write the remaining bytes of the nbw buffer */
  1209. + if (op->nbw > 0) {
  1210. + op->wb |= ((1 << 7) << (op->nbw * 8));
  1211. + ss_writer(op->wb);
  1212. + } else {
  1213. + ss_writer((1 << 7));
  1214. + }
  1215. +
  1216. + /*
  1217. + * number of space to pad to obtain 64o minus 8(size) minus 4 (final 1)
  1218. + * I take the operations from other md5/sha1 implementations
  1219. + */
  1220. +
  1221. + /* we have already send 4 more byte of which nbw data */
  1222. + if (op->mode == SS_OP_MD5) {
  1223. + index = (op->byte_count + 4) & 0x3f;
  1224. + op->byte_count += op->nbw;
  1225. + if (index > 56)
  1226. + zeros = (120 - index) / 4;
  1227. + else
  1228. + zeros = (56 - index) / 4;
  1229. + } else {
  1230. + op->byte_count += op->nbw;
  1231. + index = op->byte_count & 0x3f;
  1232. + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  1233. + zeros = (padlen - 1) / 4;
  1234. + }
  1235. + for (i = 0; i < zeros; i++)
  1236. + ss_writer(0);
  1237. +
  1238. + /* write the length of data */
  1239. + if (op->mode == SS_OP_SHA1) {
  1240. + bits = cpu_to_be64(op->byte_count << 3);
  1241. + ss_writer(bits & 0xffffffff);
  1242. + ss_writer((bits >> 32) & 0xffffffff);
  1243. + } else {
  1244. + ss_writer((op->byte_count << 3) & 0xffffffff);
  1245. + ss_writer((op->byte_count >> 29) & 0xffffffff);
  1246. + }
  1247. +
  1248. + /* Tell the SS to stop the hashing */
  1249. + writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
  1250. +
  1251. + /*
  1252. + * Wait for SS to finish the hash.
  1253. + * The timeout could happend only in case of bad overcloking
  1254. + * or driver bug.
  1255. + */
  1256. + i = 0;
  1257. + do {
  1258. + v = readl(ss->base + SS_CTL);
  1259. + i++;
  1260. + } while (i < SS_TIMEOUT && (v & SS_DATA_END) > 0);
  1261. + if (i >= SS_TIMEOUT) {
  1262. + dev_err(ss->dev, "ERROR: hash end timeout %d>%d ctl=%x len=%u\n",
  1263. + i, SS_TIMEOUT, v, areq->nbytes);
  1264. + writel(0, ss->base + SS_CTL);
  1265. + mutex_unlock(&ss->lock);
  1266. + return -EIO;
  1267. + }
  1268. +
  1269. + /* Get the hash from the device */
  1270. + if (op->mode == SS_OP_SHA1) {
  1271. + for (i = 0; i < 5; i++) {
  1272. + v = cpu_to_be32(readl(ss->base + SS_MD0 + i * 4));
  1273. + memcpy(areq->result + i * 4, &v, 4);
  1274. + }
  1275. + } else {
  1276. + for (i = 0; i < 4; i++) {
  1277. + v = readl(ss->base + SS_MD0 + i * 4);
  1278. + memcpy(areq->result + i * 4, &v, 4);
  1279. + }
  1280. + }
  1281. + writel(0, ss->base + SS_CTL);
  1282. + mutex_unlock(&ss->lock);
  1283. + return 0;
  1284. +}
  1285. +
  1286. +/* sunxi_hash_finup: finalize hashing operation after an update */
  1287. +int sunxi_hash_finup(struct ahash_request *areq)
  1288. +{
  1289. + int err;
  1290. +
  1291. + err = sunxi_hash_update(areq);
  1292. + if (err != 0)
  1293. + return err;
  1294. +
  1295. + return sunxi_hash_final(areq);
  1296. +}
  1297. +
  1298. +/* combo of init/update/final functions */
  1299. +int sunxi_hash_digest(struct ahash_request *areq)
  1300. +{
  1301. + int err;
  1302. +
  1303. + err = sunxi_hash_init(areq);
  1304. + if (err != 0)
  1305. + return err;
  1306. +
  1307. + err = sunxi_hash_update(areq);
  1308. + if (err != 0)
  1309. + return err;
  1310. +
  1311. + return sunxi_hash_final(areq);
  1312. +}
  1313. diff --git a/drivers/crypto/sunxi-ss/sunxi-ss.h b/drivers/crypto/sunxi-ss/sunxi-ss.h
  1314. new file mode 100644
  1315. index 0000000..331e75b
  1316. --- /dev/null
  1317. +++ b/drivers/crypto/sunxi-ss/sunxi-ss.h
  1318. @@ -0,0 +1,193 @@
  1319. +/*
  1320. + * sunxi-ss.c - hardware cryptographic accelerator for Allwinner A20 SoC
  1321. + *
  1322. + * Copyright (C) 2013-2014 Corentin LABBE <clabbe.montjoie@gmail.com>
  1323. + *
  1324. + * Support AES cipher with 128,192,256 bits keysize.
  1325. + * Support MD5 and SHA1 hash algorithms.
  1326. + * Support DES and 3DES
  1327. + *
  1328. + * You could find the datasheet in Documentation/arm/sunxi/README
  1329. + *
  1330. + * Licensed under the GPL-2.
  1331. + */
  1332. +
  1333. +#include <linux/clk.h>
  1334. +#include <linux/crypto.h>
  1335. +#include <linux/io.h>
  1336. +#include <linux/module.h>
  1337. +#include <linux/of.h>
  1338. +#include <linux/platform_device.h>
  1339. +#include <crypto/scatterwalk.h>
  1340. +#include <linux/scatterlist.h>
  1341. +#include <linux/interrupt.h>
  1342. +#include <linux/delay.h>
  1343. +#include <crypto/md5.h>
  1344. +#include <crypto/sha.h>
  1345. +#include <crypto/hash.h>
  1346. +#include <crypto/internal/hash.h>
  1347. +#include <crypto/aes.h>
  1348. +#include <crypto/des.h>
  1349. +#include <crypto/internal/rng.h>
  1350. +
  1351. +#define SS_CTL 0x00
  1352. +#define SS_KEY0 0x04
  1353. +#define SS_KEY1 0x08
  1354. +#define SS_KEY2 0x0C
  1355. +#define SS_KEY3 0x10
  1356. +#define SS_KEY4 0x14
  1357. +#define SS_KEY5 0x18
  1358. +#define SS_KEY6 0x1C
  1359. +#define SS_KEY7 0x20
  1360. +
  1361. +#define SS_IV0 0x24
  1362. +#define SS_IV1 0x28
  1363. +#define SS_IV2 0x2C
  1364. +#define SS_IV3 0x30
  1365. +
  1366. +#define SS_CNT0 0x34
  1367. +#define SS_CNT1 0x38
  1368. +#define SS_CNT2 0x3C
  1369. +#define SS_CNT3 0x40
  1370. +
  1371. +#define SS_FCSR 0x44
  1372. +#define SS_ICSR 0x48
  1373. +
  1374. +#define SS_MD0 0x4C
  1375. +#define SS_MD1 0x50
  1376. +#define SS_MD2 0x54
  1377. +#define SS_MD3 0x58
  1378. +#define SS_MD4 0x5C
  1379. +
  1380. +#define SS_RXFIFO 0x200
  1381. +#define SS_TXFIFO 0x204
  1382. +
  1383. +/* SS_CTL configuration values */
  1384. +
  1385. +/* PRNG generator mode - bit 15 */
  1386. +#define SS_PRNG_ONESHOT (0 << 15)
  1387. +#define SS_PRNG_CONTINUE (1 << 15)
  1388. +
  1389. +/* IV mode for hash */
  1390. +#define SS_IV_ARBITRARY (1 << 14)
  1391. +
  1392. +/* SS operation mode - bits 12-13 */
  1393. +#define SS_ECB (0 << 12)
  1394. +#define SS_CBC (1 << 12)
  1395. +#define SS_CNT (2 << 12)
  1396. +
  1397. +/* Counter width for CNT mode - bits 10-11 */
  1398. +#define SS_CNT_16BITS (0 << 10)
  1399. +#define SS_CNT_32BITS (1 << 10)
  1400. +#define SS_CNT_64BITS (2 << 10)
  1401. +
  1402. +/* Key size for AES - bits 8-9 */
  1403. +#define SS_AES_128BITS (0 << 8)
  1404. +#define SS_AES_192BITS (1 << 8)
  1405. +#define SS_AES_256BITS (2 << 8)
  1406. +
  1407. +/* Operation direction - bit 7 */
  1408. +#define SS_ENCRYPTION (0 << 7)
  1409. +#define SS_DECRYPTION (1 << 7)
  1410. +
  1411. +/* SS Method - bits 4-6 */
  1412. +#define SS_OP_AES (0 << 4)
  1413. +#define SS_OP_DES (1 << 4)
  1414. +#define SS_OP_3DES (2 << 4)
  1415. +#define SS_OP_SHA1 (3 << 4)
  1416. +#define SS_OP_MD5 (4 << 4)
  1417. +#define SS_OP_PRNG (5 << 4)
  1418. +
  1419. +/* Data end bit - bit 2 */
  1420. +#define SS_DATA_END (1 << 2)
  1421. +
  1422. +/* PRNG start bit - bit 1 */
  1423. +#define SS_PRNG_START (1 << 1)
  1424. +
  1425. +/* SS Enable bit - bit 0 */
  1426. +#define SS_DISABLED (0 << 0)
  1427. +#define SS_ENABLED (1 << 0)
  1428. +
  1429. +/* SS_FCSR configuration values */
  1430. +/* RX FIFO status - bit 30 */
  1431. +#define SS_RXFIFO_FREE (1 << 30)
  1432. +
  1433. +/* RX FIFO empty spaces - bits 24-29 */
  1434. +#define SS_RXFIFO_SPACES(val) (((val) >> 24) & 0x3f)
  1435. +
  1436. +/* TX FIFO status - bit 22 */
  1437. +#define SS_TXFIFO_AVAILABLE (1 << 22)
  1438. +
  1439. +/* TX FIFO available spaces - bits 16-21 */
  1440. +#define SS_TXFIFO_SPACES(val) (((val) >> 16) & 0x3f)
  1441. +
  1442. +#define SS_RXFIFO_EMP_INT_PENDING (1 << 10)
  1443. +#define SS_TXFIFO_AVA_INT_PENDING (1 << 8)
  1444. +#define SS_RXFIFO_EMP_INT_ENABLE (1 << 2)
  1445. +#define SS_TXFIFO_AVA_INT_ENABLE (1 << 0)
  1446. +
  1447. +/* SS_ICSR configuration values */
  1448. +#define SS_ICS_DRQ_ENABLE (1 << 4)
  1449. +
  1450. +struct sunxi_ss_ctx {
  1451. + void __iomem *base;
  1452. + int irq;
  1453. + struct clk *busclk;
  1454. + struct clk *ssclk;
  1455. + struct device *dev;
  1456. + struct resource *res;
  1457. + void *buf_in; /* pointer to data to be uploaded to the device */
  1458. + size_t buf_in_size; /* size of buf_in */
  1459. + void *buf_out;
  1460. + size_t buf_out_size;
  1461. + struct mutex lock; /* control the use of the device */
  1462. + struct mutex bufout_lock; /* control the use of buf_out*/
  1463. + struct mutex bufin_lock; /* control the sue of buf_in*/
  1464. +};
  1465. +
  1466. +struct sunxi_tfm_ctx {
  1467. + u32 key[AES_MAX_KEY_SIZE / 4];/* divided by sizeof(u32) */
  1468. + u32 keylen;
  1469. + u32 keymode;
  1470. +};
  1471. +
  1472. +struct sunxi_req_ctx {
  1473. + u32 mode;
  1474. + u64 byte_count; /* number of bytes "uploaded" to the device */
  1475. + u32 wb; /* a partial word waiting to be completed and
  1476. + uploaded to the device */
  1477. + /* number of bytes to be uploaded in the wb word */
  1478. + unsigned int nbw;
  1479. + u32 hash[5];
  1480. + u32 wait[64];
  1481. + unsigned int nwait;
  1482. +};
  1483. +
  1484. +#define SS_SEED_LEN (192/8)
  1485. +#define SS_DATA_LEN (160/8)
  1486. +
  1487. +struct prng_context {
  1488. + u32 seed[SS_SEED_LEN/4];
  1489. + unsigned int slen;
  1490. +};
  1491. +
  1492. +int sunxi_hash_crainit(struct crypto_tfm *tfm);
  1493. +int sunxi_hash_init(struct ahash_request *areq);
  1494. +int sunxi_hash_update(struct ahash_request *areq);
  1495. +int sunxi_hash_final(struct ahash_request *areq);
  1496. +int sunxi_hash_finup(struct ahash_request *areq);
  1497. +int sunxi_hash_digest(struct ahash_request *areq);
  1498. +int sunxi_hash_export(struct ahash_request *areq, void *out);
  1499. +int sunxi_hash_import(struct ahash_request *areq, const void *in);
  1500. +
  1501. +int sunxi_ss_aes_poll(struct ablkcipher_request *areq, u32 mode);
  1502. +int sunxi_ss_des_poll(struct ablkcipher_request *areq, u32 mode);
  1503. +int sunxi_ss_cipher_init(struct crypto_tfm *tfm);
  1504. +int sunxi_ss_cipher_encrypt(struct ablkcipher_request *areq);
  1505. +int sunxi_ss_cipher_decrypt(struct ablkcipher_request *areq);
  1506. +int sunxi_ss_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  1507. + unsigned int keylen);
  1508. +int sunxi_ss_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  1509. + unsigned int keylen);
  1510. +int sunxi_ss_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  1511. + unsigned int keylen);