omap-sham.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for OMAP SHA1/MD5 HW acceleration.
  5. *
  6. * Copyright (c) 2010 Nokia Corporation
  7. * Author: Dmitry Kasatkin <[email protected]>
  8. * Copyright (c) 2011 Texas Instruments Incorporated
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. *
  14. * Some ideas are from old omap-sha1-md5.c driver.
  15. */
  16. #define pr_fmt(fmt) "%s: " fmt, __func__
  17. #include <linux/err.h>
  18. #include <linux/device.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/errno.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/irq.h>
  25. #include <linux/io.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/scatterlist.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/dmaengine.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_irq.h>
  35. #include <linux/delay.h>
  36. #include <linux/crypto.h>
  37. #include <linux/cryptohash.h>
  38. #include <crypto/scatterwalk.h>
  39. #include <crypto/algapi.h>
  40. #include <crypto/sha.h>
  41. #include <crypto/hash.h>
  42. #include <crypto/internal/hash.h>
  43. #define MD5_DIGEST_SIZE 16
  44. #define SHA_REG_IDIGEST(dd, x) ((dd)->pdata->idigest_ofs + ((x)*0x04))
  45. #define SHA_REG_DIN(dd, x) ((dd)->pdata->din_ofs + ((x) * 0x04))
  46. #define SHA_REG_DIGCNT(dd) ((dd)->pdata->digcnt_ofs)
  47. #define SHA_REG_ODIGEST(dd, x) ((dd)->pdata->odigest_ofs + (x * 0x04))
  48. #define SHA_REG_CTRL 0x18
  49. #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
  50. #define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
  51. #define SHA_REG_CTRL_ALGO_CONST (1 << 3)
  52. #define SHA_REG_CTRL_ALGO (1 << 2)
  53. #define SHA_REG_CTRL_INPUT_READY (1 << 1)
  54. #define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
  55. #define SHA_REG_REV(dd) ((dd)->pdata->rev_ofs)
  56. #define SHA_REG_MASK(dd) ((dd)->pdata->mask_ofs)
  57. #define SHA_REG_MASK_DMA_EN (1 << 3)
  58. #define SHA_REG_MASK_IT_EN (1 << 2)
  59. #define SHA_REG_MASK_SOFTRESET (1 << 1)
  60. #define SHA_REG_AUTOIDLE (1 << 0)
  61. #define SHA_REG_SYSSTATUS(dd) ((dd)->pdata->sysstatus_ofs)
  62. #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
  63. #define SHA_REG_MODE(dd) ((dd)->pdata->mode_ofs)
  64. #define SHA_REG_MODE_HMAC_OUTER_HASH (1 << 7)
  65. #define SHA_REG_MODE_HMAC_KEY_PROC (1 << 5)
  66. #define SHA_REG_MODE_CLOSE_HASH (1 << 4)
  67. #define SHA_REG_MODE_ALGO_CONSTANT (1 << 3)
  68. #define SHA_REG_MODE_ALGO_MASK (7 << 0)
  69. #define SHA_REG_MODE_ALGO_MD5_128 (0 << 1)
  70. #define SHA_REG_MODE_ALGO_SHA1_160 (1 << 1)
  71. #define SHA_REG_MODE_ALGO_SHA2_224 (2 << 1)
  72. #define SHA_REG_MODE_ALGO_SHA2_256 (3 << 1)
  73. #define SHA_REG_MODE_ALGO_SHA2_384 (1 << 0)
  74. #define SHA_REG_MODE_ALGO_SHA2_512 (3 << 0)
  75. #define SHA_REG_LENGTH(dd) ((dd)->pdata->length_ofs)
  76. #define SHA_REG_IRQSTATUS 0x118
  77. #define SHA_REG_IRQSTATUS_CTX_RDY (1 << 3)
  78. #define SHA_REG_IRQSTATUS_PARTHASH_RDY (1 << 2)
  79. #define SHA_REG_IRQSTATUS_INPUT_RDY (1 << 1)
  80. #define SHA_REG_IRQSTATUS_OUTPUT_RDY (1 << 0)
  81. #define SHA_REG_IRQENA 0x11C
  82. #define SHA_REG_IRQENA_CTX_RDY (1 << 3)
  83. #define SHA_REG_IRQENA_PARTHASH_RDY (1 << 2)
  84. #define SHA_REG_IRQENA_INPUT_RDY (1 << 1)
  85. #define SHA_REG_IRQENA_OUTPUT_RDY (1 << 0)
  86. #define DEFAULT_TIMEOUT_INTERVAL HZ
  87. #define DEFAULT_AUTOSUSPEND_DELAY 1000
  88. /* mostly device flags */
  89. #define FLAGS_BUSY 0
  90. #define FLAGS_FINAL 1
  91. #define FLAGS_DMA_ACTIVE 2
  92. #define FLAGS_OUTPUT_READY 3
  93. #define FLAGS_INIT 4
  94. #define FLAGS_CPU 5
  95. #define FLAGS_DMA_READY 6
  96. #define FLAGS_AUTO_XOR 7
  97. #define FLAGS_BE32_SHA1 8
  98. #define FLAGS_SGS_COPIED 9
  99. #define FLAGS_SGS_ALLOCED 10
  100. /* context flags */
  101. #define FLAGS_FINUP 16
  102. #define FLAGS_MODE_SHIFT 18
  103. #define FLAGS_MODE_MASK (SHA_REG_MODE_ALGO_MASK << FLAGS_MODE_SHIFT)
  104. #define FLAGS_MODE_MD5 (SHA_REG_MODE_ALGO_MD5_128 << FLAGS_MODE_SHIFT)
  105. #define FLAGS_MODE_SHA1 (SHA_REG_MODE_ALGO_SHA1_160 << FLAGS_MODE_SHIFT)
  106. #define FLAGS_MODE_SHA224 (SHA_REG_MODE_ALGO_SHA2_224 << FLAGS_MODE_SHIFT)
  107. #define FLAGS_MODE_SHA256 (SHA_REG_MODE_ALGO_SHA2_256 << FLAGS_MODE_SHIFT)
  108. #define FLAGS_MODE_SHA384 (SHA_REG_MODE_ALGO_SHA2_384 << FLAGS_MODE_SHIFT)
  109. #define FLAGS_MODE_SHA512 (SHA_REG_MODE_ALGO_SHA2_512 << FLAGS_MODE_SHIFT)
  110. #define FLAGS_HMAC 21
  111. #define FLAGS_ERROR 22
  112. #define OP_UPDATE 1
  113. #define OP_FINAL 2
  114. #define OMAP_ALIGN_MASK (sizeof(u32)-1)
  115. #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
  116. #define BUFLEN SHA512_BLOCK_SIZE
  117. #define OMAP_SHA_DMA_THRESHOLD 256
  118. struct omap_sham_dev;
  119. struct omap_sham_reqctx {
  120. struct omap_sham_dev *dd;
  121. unsigned long flags;
  122. unsigned long op;
  123. u8 digest[SHA512_DIGEST_SIZE] OMAP_ALIGNED;
  124. size_t digcnt;
  125. size_t bufcnt;
  126. size_t buflen;
  127. /* walk state */
  128. struct scatterlist *sg;
  129. struct scatterlist sgl[2];
  130. int offset; /* offset in current sg */
  131. int sg_len;
  132. unsigned int total; /* total request */
  133. u8 buffer[0] OMAP_ALIGNED;
  134. };
  135. struct omap_sham_hmac_ctx {
  136. struct crypto_shash *shash;
  137. u8 ipad[SHA512_BLOCK_SIZE] OMAP_ALIGNED;
  138. u8 opad[SHA512_BLOCK_SIZE] OMAP_ALIGNED;
  139. };
  140. struct omap_sham_ctx {
  141. struct omap_sham_dev *dd;
  142. unsigned long flags;
  143. /* fallback stuff */
  144. struct crypto_shash *fallback;
  145. struct omap_sham_hmac_ctx base[0];
  146. };
  147. #define OMAP_SHAM_QUEUE_LENGTH 10
  148. struct omap_sham_algs_info {
  149. struct ahash_alg *algs_list;
  150. unsigned int size;
  151. unsigned int registered;
  152. };
  153. struct omap_sham_pdata {
  154. struct omap_sham_algs_info *algs_info;
  155. unsigned int algs_info_size;
  156. unsigned long flags;
  157. int digest_size;
  158. void (*copy_hash)(struct ahash_request *req, int out);
  159. void (*write_ctrl)(struct omap_sham_dev *dd, size_t length,
  160. int final, int dma);
  161. void (*trigger)(struct omap_sham_dev *dd, size_t length);
  162. int (*poll_irq)(struct omap_sham_dev *dd);
  163. irqreturn_t (*intr_hdlr)(int irq, void *dev_id);
  164. u32 odigest_ofs;
  165. u32 idigest_ofs;
  166. u32 din_ofs;
  167. u32 digcnt_ofs;
  168. u32 rev_ofs;
  169. u32 mask_ofs;
  170. u32 sysstatus_ofs;
  171. u32 mode_ofs;
  172. u32 length_ofs;
  173. u32 major_mask;
  174. u32 major_shift;
  175. u32 minor_mask;
  176. u32 minor_shift;
  177. };
  178. struct omap_sham_dev {
  179. struct list_head list;
  180. unsigned long phys_base;
  181. struct device *dev;
  182. void __iomem *io_base;
  183. int irq;
  184. spinlock_t lock;
  185. int err;
  186. struct dma_chan *dma_lch;
  187. struct tasklet_struct done_task;
  188. u8 polling_mode;
  189. u8 xmit_buf[BUFLEN];
  190. unsigned long flags;
  191. struct crypto_queue queue;
  192. struct ahash_request *req;
  193. const struct omap_sham_pdata *pdata;
  194. };
  195. struct omap_sham_drv {
  196. struct list_head dev_list;
  197. spinlock_t lock;
  198. unsigned long flags;
  199. };
  200. static struct omap_sham_drv sham = {
  201. .dev_list = LIST_HEAD_INIT(sham.dev_list),
  202. .lock = __SPIN_LOCK_UNLOCKED(sham.lock),
  203. };
  204. static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
  205. {
  206. return __raw_readl(dd->io_base + offset);
  207. }
  208. static inline void omap_sham_write(struct omap_sham_dev *dd,
  209. u32 offset, u32 value)
  210. {
  211. __raw_writel(value, dd->io_base + offset);
  212. }
  213. static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address,
  214. u32 value, u32 mask)
  215. {
  216. u32 val;
  217. val = omap_sham_read(dd, address);
  218. val &= ~mask;
  219. val |= value;
  220. omap_sham_write(dd, address, val);
  221. }
  222. static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit)
  223. {
  224. unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL;
  225. while (!(omap_sham_read(dd, offset) & bit)) {
  226. if (time_is_before_jiffies(timeout))
  227. return -ETIMEDOUT;
  228. }
  229. return 0;
  230. }
  231. static void omap_sham_copy_hash_omap2(struct ahash_request *req, int out)
  232. {
  233. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  234. struct omap_sham_dev *dd = ctx->dd;
  235. u32 *hash = (u32 *)ctx->digest;
  236. int i;
  237. for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
  238. if (out)
  239. hash[i] = omap_sham_read(dd, SHA_REG_IDIGEST(dd, i));
  240. else
  241. omap_sham_write(dd, SHA_REG_IDIGEST(dd, i), hash[i]);
  242. }
  243. }
  244. static void omap_sham_copy_hash_omap4(struct ahash_request *req, int out)
  245. {
  246. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  247. struct omap_sham_dev *dd = ctx->dd;
  248. int i;
  249. if (ctx->flags & BIT(FLAGS_HMAC)) {
  250. struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req);
  251. struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
  252. struct omap_sham_hmac_ctx *bctx = tctx->base;
  253. u32 *opad = (u32 *)bctx->opad;
  254. for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
  255. if (out)
  256. opad[i] = omap_sham_read(dd,
  257. SHA_REG_ODIGEST(dd, i));
  258. else
  259. omap_sham_write(dd, SHA_REG_ODIGEST(dd, i),
  260. opad[i]);
  261. }
  262. }
  263. omap_sham_copy_hash_omap2(req, out);
  264. }
  265. static void omap_sham_copy_ready_hash(struct ahash_request *req)
  266. {
  267. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  268. u32 *in = (u32 *)ctx->digest;
  269. u32 *hash = (u32 *)req->result;
  270. int i, d, big_endian = 0;
  271. if (!hash)
  272. return;
  273. switch (ctx->flags & FLAGS_MODE_MASK) {
  274. case FLAGS_MODE_MD5:
  275. d = MD5_DIGEST_SIZE / sizeof(u32);
  276. break;
  277. case FLAGS_MODE_SHA1:
  278. /* OMAP2 SHA1 is big endian */
  279. if (test_bit(FLAGS_BE32_SHA1, &ctx->dd->flags))
  280. big_endian = 1;
  281. d = SHA1_DIGEST_SIZE / sizeof(u32);
  282. break;
  283. case FLAGS_MODE_SHA224:
  284. d = SHA224_DIGEST_SIZE / sizeof(u32);
  285. break;
  286. case FLAGS_MODE_SHA256:
  287. d = SHA256_DIGEST_SIZE / sizeof(u32);
  288. break;
  289. case FLAGS_MODE_SHA384:
  290. d = SHA384_DIGEST_SIZE / sizeof(u32);
  291. break;
  292. case FLAGS_MODE_SHA512:
  293. d = SHA512_DIGEST_SIZE / sizeof(u32);
  294. break;
  295. default:
  296. d = 0;
  297. }
  298. if (big_endian)
  299. for (i = 0; i < d; i++)
  300. hash[i] = be32_to_cpu(in[i]);
  301. else
  302. for (i = 0; i < d; i++)
  303. hash[i] = le32_to_cpu(in[i]);
  304. }
  305. static int omap_sham_hw_init(struct omap_sham_dev *dd)
  306. {
  307. int err;
  308. err = pm_runtime_get_sync(dd->dev);
  309. if (err < 0) {
  310. dev_err(dd->dev, "failed to get sync: %d\n", err);
  311. return err;
  312. }
  313. if (!test_bit(FLAGS_INIT, &dd->flags)) {
  314. set_bit(FLAGS_INIT, &dd->flags);
  315. dd->err = 0;
  316. }
  317. return 0;
  318. }
  319. static void omap_sham_write_ctrl_omap2(struct omap_sham_dev *dd, size_t length,
  320. int final, int dma)
  321. {
  322. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  323. u32 val = length << 5, mask;
  324. if (likely(ctx->digcnt))
  325. omap_sham_write(dd, SHA_REG_DIGCNT(dd), ctx->digcnt);
  326. omap_sham_write_mask(dd, SHA_REG_MASK(dd),
  327. SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0),
  328. SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
  329. /*
  330. * Setting ALGO_CONST only for the first iteration
  331. * and CLOSE_HASH only for the last one.
  332. */
  333. if ((ctx->flags & FLAGS_MODE_MASK) == FLAGS_MODE_SHA1)
  334. val |= SHA_REG_CTRL_ALGO;
  335. if (!ctx->digcnt)
  336. val |= SHA_REG_CTRL_ALGO_CONST;
  337. if (final)
  338. val |= SHA_REG_CTRL_CLOSE_HASH;
  339. mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH |
  340. SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH;
  341. omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask);
  342. }
  343. static void omap_sham_trigger_omap2(struct omap_sham_dev *dd, size_t length)
  344. {
  345. }
  346. static int omap_sham_poll_irq_omap2(struct omap_sham_dev *dd)
  347. {
  348. return omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY);
  349. }
  350. static int get_block_size(struct omap_sham_reqctx *ctx)
  351. {
  352. int d;
  353. switch (ctx->flags & FLAGS_MODE_MASK) {
  354. case FLAGS_MODE_MD5:
  355. case FLAGS_MODE_SHA1:
  356. d = SHA1_BLOCK_SIZE;
  357. break;
  358. case FLAGS_MODE_SHA224:
  359. case FLAGS_MODE_SHA256:
  360. d = SHA256_BLOCK_SIZE;
  361. break;
  362. case FLAGS_MODE_SHA384:
  363. case FLAGS_MODE_SHA512:
  364. d = SHA512_BLOCK_SIZE;
  365. break;
  366. default:
  367. d = 0;
  368. }
  369. return d;
  370. }
  371. static void omap_sham_write_n(struct omap_sham_dev *dd, u32 offset,
  372. u32 *value, int count)
  373. {
  374. for (; count--; value++, offset += 4)
  375. omap_sham_write(dd, offset, *value);
  376. }
  377. static void omap_sham_write_ctrl_omap4(struct omap_sham_dev *dd, size_t length,
  378. int final, int dma)
  379. {
  380. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  381. u32 val, mask;
  382. /*
  383. * Setting ALGO_CONST only for the first iteration and
  384. * CLOSE_HASH only for the last one. Note that flags mode bits
  385. * correspond to algorithm encoding in mode register.
  386. */
  387. val = (ctx->flags & FLAGS_MODE_MASK) >> (FLAGS_MODE_SHIFT);
  388. if (!ctx->digcnt) {
  389. struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req);
  390. struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
  391. struct omap_sham_hmac_ctx *bctx = tctx->base;
  392. int bs, nr_dr;
  393. val |= SHA_REG_MODE_ALGO_CONSTANT;
  394. if (ctx->flags & BIT(FLAGS_HMAC)) {
  395. bs = get_block_size(ctx);
  396. nr_dr = bs / (2 * sizeof(u32));
  397. val |= SHA_REG_MODE_HMAC_KEY_PROC;
  398. omap_sham_write_n(dd, SHA_REG_ODIGEST(dd, 0),
  399. (u32 *)bctx->ipad, nr_dr);
  400. omap_sham_write_n(dd, SHA_REG_IDIGEST(dd, 0),
  401. (u32 *)bctx->ipad + nr_dr, nr_dr);
  402. ctx->digcnt += bs;
  403. }
  404. }
  405. if (final) {
  406. val |= SHA_REG_MODE_CLOSE_HASH;
  407. if (ctx->flags & BIT(FLAGS_HMAC))
  408. val |= SHA_REG_MODE_HMAC_OUTER_HASH;
  409. }
  410. mask = SHA_REG_MODE_ALGO_CONSTANT | SHA_REG_MODE_CLOSE_HASH |
  411. SHA_REG_MODE_ALGO_MASK | SHA_REG_MODE_HMAC_OUTER_HASH |
  412. SHA_REG_MODE_HMAC_KEY_PROC;
  413. dev_dbg(dd->dev, "ctrl: %08x, flags: %08lx\n", val, ctx->flags);
  414. omap_sham_write_mask(dd, SHA_REG_MODE(dd), val, mask);
  415. omap_sham_write(dd, SHA_REG_IRQENA, SHA_REG_IRQENA_OUTPUT_RDY);
  416. omap_sham_write_mask(dd, SHA_REG_MASK(dd),
  417. SHA_REG_MASK_IT_EN |
  418. (dma ? SHA_REG_MASK_DMA_EN : 0),
  419. SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
  420. }
  421. static void omap_sham_trigger_omap4(struct omap_sham_dev *dd, size_t length)
  422. {
  423. omap_sham_write(dd, SHA_REG_LENGTH(dd), length);
  424. }
  425. static int omap_sham_poll_irq_omap4(struct omap_sham_dev *dd)
  426. {
  427. return omap_sham_wait(dd, SHA_REG_IRQSTATUS,
  428. SHA_REG_IRQSTATUS_INPUT_RDY);
  429. }
  430. static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, size_t length,
  431. int final)
  432. {
  433. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  434. int count, len32, bs32, offset = 0;
  435. const u32 *buffer;
  436. int mlen;
  437. struct sg_mapping_iter mi;
  438. dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
  439. ctx->digcnt, length, final);
  440. dd->pdata->write_ctrl(dd, length, final, 0);
  441. dd->pdata->trigger(dd, length);
  442. /* should be non-zero before next lines to disable clocks later */
  443. ctx->digcnt += length;
  444. ctx->total -= length;
  445. if (final)
  446. set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
  447. set_bit(FLAGS_CPU, &dd->flags);
  448. len32 = DIV_ROUND_UP(length, sizeof(u32));
  449. bs32 = get_block_size(ctx) / sizeof(u32);
  450. sg_miter_start(&mi, ctx->sg, ctx->sg_len,
  451. SG_MITER_FROM_SG | SG_MITER_ATOMIC);
  452. mlen = 0;
  453. while (len32) {
  454. if (dd->pdata->poll_irq(dd))
  455. return -ETIMEDOUT;
  456. for (count = 0; count < min(len32, bs32); count++, offset++) {
  457. if (!mlen) {
  458. sg_miter_next(&mi);
  459. mlen = mi.length;
  460. if (!mlen) {
  461. pr_err("sg miter failure.\n");
  462. return -EINVAL;
  463. }
  464. offset = 0;
  465. buffer = mi.addr;
  466. }
  467. omap_sham_write(dd, SHA_REG_DIN(dd, count),
  468. buffer[offset]);
  469. mlen -= 4;
  470. }
  471. len32 -= min(len32, bs32);
  472. }
  473. sg_miter_stop(&mi);
  474. return -EINPROGRESS;
  475. }
  476. static void omap_sham_dma_callback(void *param)
  477. {
  478. struct omap_sham_dev *dd = param;
  479. set_bit(FLAGS_DMA_READY, &dd->flags);
  480. tasklet_schedule(&dd->done_task);
  481. }
  482. static int omap_sham_xmit_dma(struct omap_sham_dev *dd, size_t length,
  483. int final)
  484. {
  485. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  486. struct dma_async_tx_descriptor *tx;
  487. struct dma_slave_config cfg;
  488. int ret;
  489. dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
  490. ctx->digcnt, length, final);
  491. if (!dma_map_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE)) {
  492. dev_err(dd->dev, "dma_map_sg error\n");
  493. return -EINVAL;
  494. }
  495. memset(&cfg, 0, sizeof(cfg));
  496. cfg.dst_addr = dd->phys_base + SHA_REG_DIN(dd, 0);
  497. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  498. cfg.dst_maxburst = get_block_size(ctx) / DMA_SLAVE_BUSWIDTH_4_BYTES;
  499. ret = dmaengine_slave_config(dd->dma_lch, &cfg);
  500. if (ret) {
  501. pr_err("omap-sham: can't configure dmaengine slave: %d\n", ret);
  502. return ret;
  503. }
  504. tx = dmaengine_prep_slave_sg(dd->dma_lch, ctx->sg, ctx->sg_len,
  505. DMA_MEM_TO_DEV,
  506. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  507. if (!tx) {
  508. dev_err(dd->dev, "prep_slave_sg failed\n");
  509. return -EINVAL;
  510. }
  511. tx->callback = omap_sham_dma_callback;
  512. tx->callback_param = dd;
  513. dd->pdata->write_ctrl(dd, length, final, 1);
  514. ctx->digcnt += length;
  515. ctx->total -= length;
  516. if (final)
  517. set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
  518. set_bit(FLAGS_DMA_ACTIVE, &dd->flags);
  519. dmaengine_submit(tx);
  520. dma_async_issue_pending(dd->dma_lch);
  521. dd->pdata->trigger(dd, length);
  522. return -EINPROGRESS;
  523. }
  524. static int omap_sham_copy_sg_lists(struct omap_sham_reqctx *ctx,
  525. struct scatterlist *sg, int bs, int new_len)
  526. {
  527. int n = sg_nents(sg);
  528. struct scatterlist *tmp;
  529. int offset = ctx->offset;
  530. if (ctx->bufcnt)
  531. n++;
  532. ctx->sg = kmalloc_array(n, sizeof(*sg), GFP_KERNEL);
  533. if (!ctx->sg)
  534. return -ENOMEM;
  535. sg_init_table(ctx->sg, n);
  536. tmp = ctx->sg;
  537. ctx->sg_len = 0;
  538. if (ctx->bufcnt) {
  539. sg_set_buf(tmp, ctx->dd->xmit_buf, ctx->bufcnt);
  540. tmp = sg_next(tmp);
  541. ctx->sg_len++;
  542. }
  543. while (sg && new_len) {
  544. int len = sg->length - offset;
  545. if (offset) {
  546. offset -= sg->length;
  547. if (offset < 0)
  548. offset = 0;
  549. }
  550. if (new_len < len)
  551. len = new_len;
  552. if (len > 0) {
  553. new_len -= len;
  554. sg_set_page(tmp, sg_page(sg), len, sg->offset);
  555. if (new_len <= 0)
  556. sg_mark_end(tmp);
  557. tmp = sg_next(tmp);
  558. ctx->sg_len++;
  559. }
  560. sg = sg_next(sg);
  561. }
  562. set_bit(FLAGS_SGS_ALLOCED, &ctx->dd->flags);
  563. ctx->bufcnt = 0;
  564. return 0;
  565. }
  566. static int omap_sham_copy_sgs(struct omap_sham_reqctx *ctx,
  567. struct scatterlist *sg, int bs, int new_len)
  568. {
  569. int pages;
  570. void *buf;
  571. int len;
  572. len = new_len + ctx->bufcnt;
  573. pages = get_order(ctx->total);
  574. buf = (void *)__get_free_pages(GFP_ATOMIC, pages);
  575. if (!buf) {
  576. pr_err("Couldn't allocate pages for unaligned cases.\n");
  577. return -ENOMEM;
  578. }
  579. if (ctx->bufcnt)
  580. memcpy(buf, ctx->dd->xmit_buf, ctx->bufcnt);
  581. scatterwalk_map_and_copy(buf + ctx->bufcnt, sg, ctx->offset,
  582. ctx->total - ctx->bufcnt, 0);
  583. sg_init_table(ctx->sgl, 1);
  584. sg_set_buf(ctx->sgl, buf, len);
  585. ctx->sg = ctx->sgl;
  586. set_bit(FLAGS_SGS_COPIED, &ctx->dd->flags);
  587. ctx->sg_len = 1;
  588. ctx->bufcnt = 0;
  589. ctx->offset = 0;
  590. return 0;
  591. }
  592. static int omap_sham_align_sgs(struct scatterlist *sg,
  593. int nbytes, int bs, bool final,
  594. struct omap_sham_reqctx *rctx)
  595. {
  596. int n = 0;
  597. bool aligned = true;
  598. bool list_ok = true;
  599. struct scatterlist *sg_tmp = sg;
  600. int new_len;
  601. int offset = rctx->offset;
  602. if (!sg || !sg->length || !nbytes)
  603. return 0;
  604. new_len = nbytes;
  605. if (offset)
  606. list_ok = false;
  607. if (final)
  608. new_len = DIV_ROUND_UP(new_len, bs) * bs;
  609. else
  610. new_len = (new_len - 1) / bs * bs;
  611. if (nbytes != new_len)
  612. list_ok = false;
  613. while (nbytes > 0 && sg_tmp) {
  614. n++;
  615. if (offset < sg_tmp->length) {
  616. if (!IS_ALIGNED(offset + sg_tmp->offset, 4)) {
  617. aligned = false;
  618. break;
  619. }
  620. if (!IS_ALIGNED(sg_tmp->length - offset, bs)) {
  621. aligned = false;
  622. break;
  623. }
  624. }
  625. if (offset) {
  626. offset -= sg_tmp->length;
  627. if (offset < 0) {
  628. nbytes += offset;
  629. offset = 0;
  630. }
  631. } else {
  632. nbytes -= sg_tmp->length;
  633. }
  634. sg_tmp = sg_next(sg_tmp);
  635. if (nbytes < 0) {
  636. list_ok = false;
  637. break;
  638. }
  639. }
  640. if (!aligned)
  641. return omap_sham_copy_sgs(rctx, sg, bs, new_len);
  642. else if (!list_ok)
  643. return omap_sham_copy_sg_lists(rctx, sg, bs, new_len);
  644. rctx->sg_len = n;
  645. rctx->sg = sg;
  646. return 0;
  647. }
  648. static int omap_sham_prepare_request(struct ahash_request *req, bool update)
  649. {
  650. struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
  651. int bs;
  652. int ret;
  653. int nbytes;
  654. bool final = rctx->flags & BIT(FLAGS_FINUP);
  655. int xmit_len, hash_later;
  656. if (!req)
  657. return 0;
  658. bs = get_block_size(rctx);
  659. if (update)
  660. nbytes = req->nbytes;
  661. else
  662. nbytes = 0;
  663. rctx->total = nbytes + rctx->bufcnt;
  664. if (!rctx->total)
  665. return 0;
  666. if (nbytes && (!IS_ALIGNED(rctx->bufcnt, bs))) {
  667. int len = bs - rctx->bufcnt % bs;
  668. if (len > nbytes)
  669. len = nbytes;
  670. scatterwalk_map_and_copy(rctx->buffer + rctx->bufcnt, req->src,
  671. 0, len, 0);
  672. rctx->bufcnt += len;
  673. nbytes -= len;
  674. rctx->offset = len;
  675. }
  676. if (rctx->bufcnt)
  677. memcpy(rctx->dd->xmit_buf, rctx->buffer, rctx->bufcnt);
  678. ret = omap_sham_align_sgs(req->src, nbytes, bs, final, rctx);
  679. if (ret)
  680. return ret;
  681. xmit_len = rctx->total;
  682. if (!IS_ALIGNED(xmit_len, bs)) {
  683. if (final)
  684. xmit_len = DIV_ROUND_UP(xmit_len, bs) * bs;
  685. else
  686. xmit_len = xmit_len / bs * bs;
  687. } else if (!final) {
  688. xmit_len -= bs;
  689. }
  690. hash_later = rctx->total - xmit_len;
  691. if (hash_later < 0)
  692. hash_later = 0;
  693. if (rctx->bufcnt && nbytes) {
  694. /* have data from previous operation and current */
  695. sg_init_table(rctx->sgl, 2);
  696. sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, rctx->bufcnt);
  697. sg_chain(rctx->sgl, 2, req->src);
  698. rctx->sg = rctx->sgl;
  699. rctx->sg_len++;
  700. } else if (rctx->bufcnt) {
  701. /* have buffered data only */
  702. sg_init_table(rctx->sgl, 1);
  703. sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, xmit_len);
  704. rctx->sg = rctx->sgl;
  705. rctx->sg_len = 1;
  706. }
  707. if (hash_later) {
  708. int offset = 0;
  709. if (hash_later > req->nbytes) {
  710. memcpy(rctx->buffer, rctx->buffer + xmit_len,
  711. hash_later - req->nbytes);
  712. offset = hash_later - req->nbytes;
  713. }
  714. if (req->nbytes) {
  715. scatterwalk_map_and_copy(rctx->buffer + offset,
  716. req->src,
  717. offset + req->nbytes -
  718. hash_later, hash_later, 0);
  719. }
  720. rctx->bufcnt = hash_later;
  721. } else {
  722. rctx->bufcnt = 0;
  723. }
  724. if (!final)
  725. rctx->total = xmit_len;
  726. return 0;
  727. }
  728. static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
  729. {
  730. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  731. dma_unmap_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE);
  732. clear_bit(FLAGS_DMA_ACTIVE, &dd->flags);
  733. return 0;
  734. }
  735. static int omap_sham_init(struct ahash_request *req)
  736. {
  737. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  738. struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
  739. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  740. struct omap_sham_dev *dd = NULL, *tmp;
  741. int bs = 0;
  742. spin_lock_bh(&sham.lock);
  743. if (!tctx->dd) {
  744. list_for_each_entry(tmp, &sham.dev_list, list) {
  745. dd = tmp;
  746. break;
  747. }
  748. tctx->dd = dd;
  749. } else {
  750. dd = tctx->dd;
  751. }
  752. spin_unlock_bh(&sham.lock);
  753. ctx->dd = dd;
  754. ctx->flags = 0;
  755. dev_dbg(dd->dev, "init: digest size: %d\n",
  756. crypto_ahash_digestsize(tfm));
  757. switch (crypto_ahash_digestsize(tfm)) {
  758. case MD5_DIGEST_SIZE:
  759. ctx->flags |= FLAGS_MODE_MD5;
  760. bs = SHA1_BLOCK_SIZE;
  761. break;
  762. case SHA1_DIGEST_SIZE:
  763. ctx->flags |= FLAGS_MODE_SHA1;
  764. bs = SHA1_BLOCK_SIZE;
  765. break;
  766. case SHA224_DIGEST_SIZE:
  767. ctx->flags |= FLAGS_MODE_SHA224;
  768. bs = SHA224_BLOCK_SIZE;
  769. break;
  770. case SHA256_DIGEST_SIZE:
  771. ctx->flags |= FLAGS_MODE_SHA256;
  772. bs = SHA256_BLOCK_SIZE;
  773. break;
  774. case SHA384_DIGEST_SIZE:
  775. ctx->flags |= FLAGS_MODE_SHA384;
  776. bs = SHA384_BLOCK_SIZE;
  777. break;
  778. case SHA512_DIGEST_SIZE:
  779. ctx->flags |= FLAGS_MODE_SHA512;
  780. bs = SHA512_BLOCK_SIZE;
  781. break;
  782. }
  783. ctx->bufcnt = 0;
  784. ctx->digcnt = 0;
  785. ctx->total = 0;
  786. ctx->offset = 0;
  787. ctx->buflen = BUFLEN;
  788. if (tctx->flags & BIT(FLAGS_HMAC)) {
  789. if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
  790. struct omap_sham_hmac_ctx *bctx = tctx->base;
  791. memcpy(ctx->buffer, bctx->ipad, bs);
  792. ctx->bufcnt = bs;
  793. }
  794. ctx->flags |= BIT(FLAGS_HMAC);
  795. }
  796. return 0;
  797. }
  798. static int omap_sham_update_req(struct omap_sham_dev *dd)
  799. {
  800. struct ahash_request *req = dd->req;
  801. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  802. int err;
  803. bool final = ctx->flags & BIT(FLAGS_FINUP);
  804. dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n",
  805. ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0);
  806. if (ctx->total < get_block_size(ctx) ||
  807. ctx->total < OMAP_SHA_DMA_THRESHOLD)
  808. ctx->flags |= BIT(FLAGS_CPU);
  809. if (ctx->flags & BIT(FLAGS_CPU))
  810. err = omap_sham_xmit_cpu(dd, ctx->total, final);
  811. else
  812. err = omap_sham_xmit_dma(dd, ctx->total, final);
  813. /* wait for dma completion before can take more data */
  814. dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt);
  815. return err;
  816. }
  817. static int omap_sham_final_req(struct omap_sham_dev *dd)
  818. {
  819. struct ahash_request *req = dd->req;
  820. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  821. int err = 0, use_dma = 1;
  822. if ((ctx->total <= get_block_size(ctx)) || dd->polling_mode)
  823. /*
  824. * faster to handle last block with cpu or
  825. * use cpu when dma is not present.
  826. */
  827. use_dma = 0;
  828. if (use_dma)
  829. err = omap_sham_xmit_dma(dd, ctx->total, 1);
  830. else
  831. err = omap_sham_xmit_cpu(dd, ctx->total, 1);
  832. ctx->bufcnt = 0;
  833. dev_dbg(dd->dev, "final_req: err: %d\n", err);
  834. return err;
  835. }
  836. static int omap_sham_finish_hmac(struct ahash_request *req)
  837. {
  838. struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  839. struct omap_sham_hmac_ctx *bctx = tctx->base;
  840. int bs = crypto_shash_blocksize(bctx->shash);
  841. int ds = crypto_shash_digestsize(bctx->shash);
  842. SHASH_DESC_ON_STACK(shash, bctx->shash);
  843. shash->tfm = bctx->shash;
  844. shash->flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
  845. return crypto_shash_init(shash) ?:
  846. crypto_shash_update(shash, bctx->opad, bs) ?:
  847. crypto_shash_finup(shash, req->result, ds, req->result);
  848. }
  849. static int omap_sham_finish(struct ahash_request *req)
  850. {
  851. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  852. struct omap_sham_dev *dd = ctx->dd;
  853. int err = 0;
  854. if (ctx->digcnt) {
  855. omap_sham_copy_ready_hash(req);
  856. if ((ctx->flags & BIT(FLAGS_HMAC)) &&
  857. !test_bit(FLAGS_AUTO_XOR, &dd->flags))
  858. err = omap_sham_finish_hmac(req);
  859. }
  860. dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt);
  861. return err;
  862. }
  863. static void omap_sham_finish_req(struct ahash_request *req, int err)
  864. {
  865. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  866. struct omap_sham_dev *dd = ctx->dd;
  867. if (test_bit(FLAGS_SGS_COPIED, &dd->flags))
  868. free_pages((unsigned long)sg_virt(ctx->sg),
  869. get_order(ctx->sg->length + ctx->bufcnt));
  870. if (test_bit(FLAGS_SGS_ALLOCED, &dd->flags))
  871. kfree(ctx->sg);
  872. ctx->sg = NULL;
  873. dd->flags &= ~(BIT(FLAGS_SGS_ALLOCED) | BIT(FLAGS_SGS_COPIED));
  874. if (!err) {
  875. dd->pdata->copy_hash(req, 1);
  876. if (test_bit(FLAGS_FINAL, &dd->flags))
  877. err = omap_sham_finish(req);
  878. } else {
  879. ctx->flags |= BIT(FLAGS_ERROR);
  880. }
  881. /* atomic operation is not needed here */
  882. dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) |
  883. BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY));
  884. pm_runtime_mark_last_busy(dd->dev);
  885. pm_runtime_put_autosuspend(dd->dev);
  886. if (req->base.complete)
  887. req->base.complete(&req->base, err);
  888. }
  889. static int omap_sham_handle_queue(struct omap_sham_dev *dd,
  890. struct ahash_request *req)
  891. {
  892. struct crypto_async_request *async_req, *backlog;
  893. struct omap_sham_reqctx *ctx;
  894. unsigned long flags;
  895. int err = 0, ret = 0;
  896. retry:
  897. spin_lock_irqsave(&dd->lock, flags);
  898. if (req)
  899. ret = ahash_enqueue_request(&dd->queue, req);
  900. if (test_bit(FLAGS_BUSY, &dd->flags)) {
  901. spin_unlock_irqrestore(&dd->lock, flags);
  902. return ret;
  903. }
  904. backlog = crypto_get_backlog(&dd->queue);
  905. async_req = crypto_dequeue_request(&dd->queue);
  906. if (async_req)
  907. set_bit(FLAGS_BUSY, &dd->flags);
  908. spin_unlock_irqrestore(&dd->lock, flags);
  909. if (!async_req)
  910. return ret;
  911. if (backlog)
  912. backlog->complete(backlog, -EINPROGRESS);
  913. req = ahash_request_cast(async_req);
  914. dd->req = req;
  915. ctx = ahash_request_ctx(req);
  916. err = omap_sham_prepare_request(req, ctx->op == OP_UPDATE);
  917. if (err || !ctx->total)
  918. goto err1;
  919. dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
  920. ctx->op, req->nbytes);
  921. err = omap_sham_hw_init(dd);
  922. if (err)
  923. goto err1;
  924. if (ctx->digcnt)
  925. /* request has changed - restore hash */
  926. dd->pdata->copy_hash(req, 0);
  927. if (ctx->op == OP_UPDATE) {
  928. err = omap_sham_update_req(dd);
  929. if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP)))
  930. /* no final() after finup() */
  931. err = omap_sham_final_req(dd);
  932. } else if (ctx->op == OP_FINAL) {
  933. err = omap_sham_final_req(dd);
  934. }
  935. err1:
  936. dev_dbg(dd->dev, "exit, err: %d\n", err);
  937. if (err != -EINPROGRESS) {
  938. /* done_task will not finish it, so do it here */
  939. omap_sham_finish_req(req, err);
  940. req = NULL;
  941. /*
  942. * Execute next request immediately if there is anything
  943. * in queue.
  944. */
  945. goto retry;
  946. }
  947. return ret;
  948. }
  949. static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
  950. {
  951. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  952. struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  953. struct omap_sham_dev *dd = tctx->dd;
  954. ctx->op = op;
  955. return omap_sham_handle_queue(dd, req);
  956. }
  957. static int omap_sham_update(struct ahash_request *req)
  958. {
  959. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  960. struct omap_sham_dev *dd = ctx->dd;
  961. if (!req->nbytes)
  962. return 0;
  963. if (ctx->bufcnt + req->nbytes <= ctx->buflen) {
  964. scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, req->src,
  965. 0, req->nbytes, 0);
  966. ctx->bufcnt += req->nbytes;
  967. return 0;
  968. }
  969. if (dd->polling_mode)
  970. ctx->flags |= BIT(FLAGS_CPU);
  971. return omap_sham_enqueue(req, OP_UPDATE);
  972. }
  973. static int omap_sham_shash_digest(struct crypto_shash *tfm, u32 flags,
  974. const u8 *data, unsigned int len, u8 *out)
  975. {
  976. SHASH_DESC_ON_STACK(shash, tfm);
  977. shash->tfm = tfm;
  978. shash->flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  979. return crypto_shash_digest(shash, data, len, out);
  980. }
  981. static int omap_sham_final_shash(struct ahash_request *req)
  982. {
  983. struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  984. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  985. int offset = 0;
  986. /*
  987. * If we are running HMAC on limited hardware support, skip
  988. * the ipad in the beginning of the buffer if we are going for
  989. * software fallback algorithm.
  990. */
  991. if (test_bit(FLAGS_HMAC, &ctx->flags) &&
  992. !test_bit(FLAGS_AUTO_XOR, &ctx->dd->flags))
  993. offset = get_block_size(ctx);
  994. return omap_sham_shash_digest(tctx->fallback, req->base.flags,
  995. ctx->buffer + offset,
  996. ctx->bufcnt - offset, req->result);
  997. }
  998. static int omap_sham_final(struct ahash_request *req)
  999. {
  1000. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  1001. ctx->flags |= BIT(FLAGS_FINUP);
  1002. if (ctx->flags & BIT(FLAGS_ERROR))
  1003. return 0; /* uncompleted hash is not needed */
  1004. /*
  1005. * OMAP HW accel works only with buffers >= 9.
  1006. * HMAC is always >= 9 because ipad == block size.
  1007. * If buffersize is less than DMA_THRESHOLD, we use fallback
  1008. * SW encoding, as using DMA + HW in this case doesn't provide
  1009. * any benefit.
  1010. */
  1011. if (!ctx->digcnt && ctx->bufcnt < OMAP_SHA_DMA_THRESHOLD)
  1012. return omap_sham_final_shash(req);
  1013. else if (ctx->bufcnt)
  1014. return omap_sham_enqueue(req, OP_FINAL);
  1015. /* copy ready hash (+ finalize hmac) */
  1016. return omap_sham_finish(req);
  1017. }
  1018. static int omap_sham_finup(struct ahash_request *req)
  1019. {
  1020. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  1021. int err1, err2;
  1022. ctx->flags |= BIT(FLAGS_FINUP);
  1023. err1 = omap_sham_update(req);
  1024. if (err1 == -EINPROGRESS || err1 == -EBUSY)
  1025. return err1;
  1026. /*
  1027. * final() has to be always called to cleanup resources
  1028. * even if udpate() failed, except EINPROGRESS
  1029. */
  1030. err2 = omap_sham_final(req);
  1031. return err1 ?: err2;
  1032. }
  1033. static int omap_sham_digest(struct ahash_request *req)
  1034. {
  1035. return omap_sham_init(req) ?: omap_sham_finup(req);
  1036. }
  1037. static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
  1038. unsigned int keylen)
  1039. {
  1040. struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
  1041. struct omap_sham_hmac_ctx *bctx = tctx->base;
  1042. int bs = crypto_shash_blocksize(bctx->shash);
  1043. int ds = crypto_shash_digestsize(bctx->shash);
  1044. struct omap_sham_dev *dd = NULL, *tmp;
  1045. int err, i;
  1046. spin_lock_bh(&sham.lock);
  1047. if (!tctx->dd) {
  1048. list_for_each_entry(tmp, &sham.dev_list, list) {
  1049. dd = tmp;
  1050. break;
  1051. }
  1052. tctx->dd = dd;
  1053. } else {
  1054. dd = tctx->dd;
  1055. }
  1056. spin_unlock_bh(&sham.lock);
  1057. err = crypto_shash_setkey(tctx->fallback, key, keylen);
  1058. if (err)
  1059. return err;
  1060. if (keylen > bs) {
  1061. err = omap_sham_shash_digest(bctx->shash,
  1062. crypto_shash_get_flags(bctx->shash),
  1063. key, keylen, bctx->ipad);
  1064. if (err)
  1065. return err;
  1066. keylen = ds;
  1067. } else {
  1068. memcpy(bctx->ipad, key, keylen);
  1069. }
  1070. memset(bctx->ipad + keylen, 0, bs - keylen);
  1071. if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
  1072. memcpy(bctx->opad, bctx->ipad, bs);
  1073. for (i = 0; i < bs; i++) {
  1074. bctx->ipad[i] ^= 0x36;
  1075. bctx->opad[i] ^= 0x5c;
  1076. }
  1077. }
  1078. return err;
  1079. }
  1080. static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
  1081. {
  1082. struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
  1083. const char *alg_name = crypto_tfm_alg_name(tfm);
  1084. /* Allocate a fallback and abort if it failed. */
  1085. tctx->fallback = crypto_alloc_shash(alg_name, 0,
  1086. CRYPTO_ALG_NEED_FALLBACK);
  1087. if (IS_ERR(tctx->fallback)) {
  1088. pr_err("omap-sham: fallback driver '%s' "
  1089. "could not be loaded.\n", alg_name);
  1090. return PTR_ERR(tctx->fallback);
  1091. }
  1092. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1093. sizeof(struct omap_sham_reqctx) + BUFLEN);
  1094. if (alg_base) {
  1095. struct omap_sham_hmac_ctx *bctx = tctx->base;
  1096. tctx->flags |= BIT(FLAGS_HMAC);
  1097. bctx->shash = crypto_alloc_shash(alg_base, 0,
  1098. CRYPTO_ALG_NEED_FALLBACK);
  1099. if (IS_ERR(bctx->shash)) {
  1100. pr_err("omap-sham: base driver '%s' "
  1101. "could not be loaded.\n", alg_base);
  1102. crypto_free_shash(tctx->fallback);
  1103. return PTR_ERR(bctx->shash);
  1104. }
  1105. }
  1106. return 0;
  1107. }
  1108. static int omap_sham_cra_init(struct crypto_tfm *tfm)
  1109. {
  1110. return omap_sham_cra_init_alg(tfm, NULL);
  1111. }
  1112. static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
  1113. {
  1114. return omap_sham_cra_init_alg(tfm, "sha1");
  1115. }
  1116. static int omap_sham_cra_sha224_init(struct crypto_tfm *tfm)
  1117. {
  1118. return omap_sham_cra_init_alg(tfm, "sha224");
  1119. }
  1120. static int omap_sham_cra_sha256_init(struct crypto_tfm *tfm)
  1121. {
  1122. return omap_sham_cra_init_alg(tfm, "sha256");
  1123. }
  1124. static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
  1125. {
  1126. return omap_sham_cra_init_alg(tfm, "md5");
  1127. }
  1128. static int omap_sham_cra_sha384_init(struct crypto_tfm *tfm)
  1129. {
  1130. return omap_sham_cra_init_alg(tfm, "sha384");
  1131. }
  1132. static int omap_sham_cra_sha512_init(struct crypto_tfm *tfm)
  1133. {
  1134. return omap_sham_cra_init_alg(tfm, "sha512");
  1135. }
  1136. static void omap_sham_cra_exit(struct crypto_tfm *tfm)
  1137. {
  1138. struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
  1139. crypto_free_shash(tctx->fallback);
  1140. tctx->fallback = NULL;
  1141. if (tctx->flags & BIT(FLAGS_HMAC)) {
  1142. struct omap_sham_hmac_ctx *bctx = tctx->base;
  1143. crypto_free_shash(bctx->shash);
  1144. }
  1145. }
  1146. static int omap_sham_export(struct ahash_request *req, void *out)
  1147. {
  1148. struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
  1149. memcpy(out, rctx, sizeof(*rctx) + rctx->bufcnt);
  1150. return 0;
  1151. }
  1152. static int omap_sham_import(struct ahash_request *req, const void *in)
  1153. {
  1154. struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
  1155. const struct omap_sham_reqctx *ctx_in = in;
  1156. memcpy(rctx, in, sizeof(*rctx) + ctx_in->bufcnt);
  1157. return 0;
  1158. }
  1159. static struct ahash_alg algs_sha1_md5[] = {
  1160. {
  1161. .init = omap_sham_init,
  1162. .update = omap_sham_update,
  1163. .final = omap_sham_final,
  1164. .finup = omap_sham_finup,
  1165. .digest = omap_sham_digest,
  1166. .halg.digestsize = SHA1_DIGEST_SIZE,
  1167. .halg.base = {
  1168. .cra_name = "sha1",
  1169. .cra_driver_name = "omap-sha1",
  1170. .cra_priority = 400,
  1171. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1172. CRYPTO_ALG_KERN_DRIVER_ONLY |
  1173. CRYPTO_ALG_ASYNC |
  1174. CRYPTO_ALG_NEED_FALLBACK,
  1175. .cra_blocksize = SHA1_BLOCK_SIZE,
  1176. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  1177. .cra_alignmask = OMAP_ALIGN_MASK,
  1178. .cra_module = THIS_MODULE,
  1179. .cra_init = omap_sham_cra_init,
  1180. .cra_exit = omap_sham_cra_exit,
  1181. }
  1182. },
  1183. {
  1184. .init = omap_sham_init,
  1185. .update = omap_sham_update,
  1186. .final = omap_sham_final,
  1187. .finup = omap_sham_finup,
  1188. .digest = omap_sham_digest,
  1189. .halg.digestsize = MD5_DIGEST_SIZE,
  1190. .halg.base = {
  1191. .cra_name = "md5",
  1192. .cra_driver_name = "omap-md5",
  1193. .cra_priority = 400,
  1194. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1195. CRYPTO_ALG_KERN_DRIVER_ONLY |
  1196. CRYPTO_ALG_ASYNC |
  1197. CRYPTO_ALG_NEED_FALLBACK,
  1198. .cra_blocksize = SHA1_BLOCK_SIZE,
  1199. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  1200. .cra_alignmask = OMAP_ALIGN_MASK,
  1201. .cra_module = THIS_MODULE,
  1202. .cra_init = omap_sham_cra_init,
  1203. .cra_exit = omap_sham_cra_exit,
  1204. }
  1205. },
  1206. {
  1207. .init = omap_sham_init,
  1208. .update = omap_sham_update,
  1209. .final = omap_sham_final,
  1210. .finup = omap_sham_finup,
  1211. .digest = omap_sham_digest,
  1212. .setkey = omap_sham_setkey,
  1213. .halg.digestsize = SHA1_DIGEST_SIZE,
  1214. .halg.base = {
  1215. .cra_name = "hmac(sha1)",
  1216. .cra_driver_name = "omap-hmac-sha1",
  1217. .cra_priority = 400,
  1218. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1219. CRYPTO_ALG_KERN_DRIVER_ONLY |
  1220. CRYPTO_ALG_ASYNC |
  1221. CRYPTO_ALG_NEED_FALLBACK,
  1222. .cra_blocksize = SHA1_BLOCK_SIZE,
  1223. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  1224. sizeof(struct omap_sham_hmac_ctx),
  1225. .cra_alignmask = OMAP_ALIGN_MASK,
  1226. .cra_module = THIS_MODULE,
  1227. .cra_init = omap_sham_cra_sha1_init,
  1228. .cra_exit = omap_sham_cra_exit,
  1229. }
  1230. },
  1231. {
  1232. .init = omap_sham_init,
  1233. .update = omap_sham_update,
  1234. .final = omap_sham_final,
  1235. .finup = omap_sham_finup,
  1236. .digest = omap_sham_digest,
  1237. .setkey = omap_sham_setkey,
  1238. .halg.digestsize = MD5_DIGEST_SIZE,
  1239. .halg.base = {
  1240. .cra_name = "hmac(md5)",
  1241. .cra_driver_name = "omap-hmac-md5",
  1242. .cra_priority = 400,
  1243. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1244. CRYPTO_ALG_KERN_DRIVER_ONLY |
  1245. CRYPTO_ALG_ASYNC |
  1246. CRYPTO_ALG_NEED_FALLBACK,
  1247. .cra_blocksize = SHA1_BLOCK_SIZE,
  1248. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  1249. sizeof(struct omap_sham_hmac_ctx),
  1250. .cra_alignmask = OMAP_ALIGN_MASK,
  1251. .cra_module = THIS_MODULE,
  1252. .cra_init = omap_sham_cra_md5_init,
  1253. .cra_exit = omap_sham_cra_exit,
  1254. }
  1255. }
  1256. };
  1257. /* OMAP4 has some algs in addition to what OMAP2 has */
  1258. static struct ahash_alg algs_sha224_sha256[] = {
  1259. {
  1260. .init = omap_sham_init,
  1261. .update = omap_sham_update,
  1262. .final = omap_sham_final,
  1263. .finup = omap_sham_finup,
  1264. .digest = omap_sham_digest,
  1265. .halg.digestsize = SHA224_DIGEST_SIZE,
  1266. .halg.base = {
  1267. .cra_name = "sha224",
  1268. .cra_driver_name = "omap-sha224",
  1269. .cra_priority = 400,
  1270. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1271. CRYPTO_ALG_ASYNC |
  1272. CRYPTO_ALG_NEED_FALLBACK,
  1273. .cra_blocksize = SHA224_BLOCK_SIZE,
  1274. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  1275. .cra_alignmask = OMAP_ALIGN_MASK,
  1276. .cra_module = THIS_MODULE,
  1277. .cra_init = omap_sham_cra_init,
  1278. .cra_exit = omap_sham_cra_exit,
  1279. }
  1280. },
  1281. {
  1282. .init = omap_sham_init,
  1283. .update = omap_sham_update,
  1284. .final = omap_sham_final,
  1285. .finup = omap_sham_finup,
  1286. .digest = omap_sham_digest,
  1287. .halg.digestsize = SHA256_DIGEST_SIZE,
  1288. .halg.base = {
  1289. .cra_name = "sha256",
  1290. .cra_driver_name = "omap-sha256",
  1291. .cra_priority = 400,
  1292. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1293. CRYPTO_ALG_ASYNC |
  1294. CRYPTO_ALG_NEED_FALLBACK,
  1295. .cra_blocksize = SHA256_BLOCK_SIZE,
  1296. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  1297. .cra_alignmask = OMAP_ALIGN_MASK,
  1298. .cra_module = THIS_MODULE,
  1299. .cra_init = omap_sham_cra_init,
  1300. .cra_exit = omap_sham_cra_exit,
  1301. }
  1302. },
  1303. {
  1304. .init = omap_sham_init,
  1305. .update = omap_sham_update,
  1306. .final = omap_sham_final,
  1307. .finup = omap_sham_finup,
  1308. .digest = omap_sham_digest,
  1309. .setkey = omap_sham_setkey,
  1310. .halg.digestsize = SHA224_DIGEST_SIZE,
  1311. .halg.base = {
  1312. .cra_name = "hmac(sha224)",
  1313. .cra_driver_name = "omap-hmac-sha224",
  1314. .cra_priority = 400,
  1315. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1316. CRYPTO_ALG_ASYNC |
  1317. CRYPTO_ALG_NEED_FALLBACK,
  1318. .cra_blocksize = SHA224_BLOCK_SIZE,
  1319. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  1320. sizeof(struct omap_sham_hmac_ctx),
  1321. .cra_alignmask = OMAP_ALIGN_MASK,
  1322. .cra_module = THIS_MODULE,
  1323. .cra_init = omap_sham_cra_sha224_init,
  1324. .cra_exit = omap_sham_cra_exit,
  1325. }
  1326. },
  1327. {
  1328. .init = omap_sham_init,
  1329. .update = omap_sham_update,
  1330. .final = omap_sham_final,
  1331. .finup = omap_sham_finup,
  1332. .digest = omap_sham_digest,
  1333. .setkey = omap_sham_setkey,
  1334. .halg.digestsize = SHA256_DIGEST_SIZE,
  1335. .halg.base = {
  1336. .cra_name = "hmac(sha256)",
  1337. .cra_driver_name = "omap-hmac-sha256",
  1338. .cra_priority = 400,
  1339. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1340. CRYPTO_ALG_ASYNC |
  1341. CRYPTO_ALG_NEED_FALLBACK,
  1342. .cra_blocksize = SHA256_BLOCK_SIZE,
  1343. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  1344. sizeof(struct omap_sham_hmac_ctx),
  1345. .cra_alignmask = OMAP_ALIGN_MASK,
  1346. .cra_module = THIS_MODULE,
  1347. .cra_init = omap_sham_cra_sha256_init,
  1348. .cra_exit = omap_sham_cra_exit,
  1349. }
  1350. },
  1351. };
  1352. static struct ahash_alg algs_sha384_sha512[] = {
  1353. {
  1354. .init = omap_sham_init,
  1355. .update = omap_sham_update,
  1356. .final = omap_sham_final,
  1357. .finup = omap_sham_finup,
  1358. .digest = omap_sham_digest,
  1359. .halg.digestsize = SHA384_DIGEST_SIZE,
  1360. .halg.base = {
  1361. .cra_name = "sha384",
  1362. .cra_driver_name = "omap-sha384",
  1363. .cra_priority = 400,
  1364. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1365. CRYPTO_ALG_ASYNC |
  1366. CRYPTO_ALG_NEED_FALLBACK,
  1367. .cra_blocksize = SHA384_BLOCK_SIZE,
  1368. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  1369. .cra_alignmask = OMAP_ALIGN_MASK,
  1370. .cra_module = THIS_MODULE,
  1371. .cra_init = omap_sham_cra_init,
  1372. .cra_exit = omap_sham_cra_exit,
  1373. }
  1374. },
  1375. {
  1376. .init = omap_sham_init,
  1377. .update = omap_sham_update,
  1378. .final = omap_sham_final,
  1379. .finup = omap_sham_finup,
  1380. .digest = omap_sham_digest,
  1381. .halg.digestsize = SHA512_DIGEST_SIZE,
  1382. .halg.base = {
  1383. .cra_name = "sha512",
  1384. .cra_driver_name = "omap-sha512",
  1385. .cra_priority = 400,
  1386. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1387. CRYPTO_ALG_ASYNC |
  1388. CRYPTO_ALG_NEED_FALLBACK,
  1389. .cra_blocksize = SHA512_BLOCK_SIZE,
  1390. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  1391. .cra_alignmask = OMAP_ALIGN_MASK,
  1392. .cra_module = THIS_MODULE,
  1393. .cra_init = omap_sham_cra_init,
  1394. .cra_exit = omap_sham_cra_exit,
  1395. }
  1396. },
  1397. {
  1398. .init = omap_sham_init,
  1399. .update = omap_sham_update,
  1400. .final = omap_sham_final,
  1401. .finup = omap_sham_finup,
  1402. .digest = omap_sham_digest,
  1403. .setkey = omap_sham_setkey,
  1404. .halg.digestsize = SHA384_DIGEST_SIZE,
  1405. .halg.base = {
  1406. .cra_name = "hmac(sha384)",
  1407. .cra_driver_name = "omap-hmac-sha384",
  1408. .cra_priority = 400,
  1409. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1410. CRYPTO_ALG_ASYNC |
  1411. CRYPTO_ALG_NEED_FALLBACK,
  1412. .cra_blocksize = SHA384_BLOCK_SIZE,
  1413. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  1414. sizeof(struct omap_sham_hmac_ctx),
  1415. .cra_alignmask = OMAP_ALIGN_MASK,
  1416. .cra_module = THIS_MODULE,
  1417. .cra_init = omap_sham_cra_sha384_init,
  1418. .cra_exit = omap_sham_cra_exit,
  1419. }
  1420. },
  1421. {
  1422. .init = omap_sham_init,
  1423. .update = omap_sham_update,
  1424. .final = omap_sham_final,
  1425. .finup = omap_sham_finup,
  1426. .digest = omap_sham_digest,
  1427. .setkey = omap_sham_setkey,
  1428. .halg.digestsize = SHA512_DIGEST_SIZE,
  1429. .halg.base = {
  1430. .cra_name = "hmac(sha512)",
  1431. .cra_driver_name = "omap-hmac-sha512",
  1432. .cra_priority = 400,
  1433. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1434. CRYPTO_ALG_ASYNC |
  1435. CRYPTO_ALG_NEED_FALLBACK,
  1436. .cra_blocksize = SHA512_BLOCK_SIZE,
  1437. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  1438. sizeof(struct omap_sham_hmac_ctx),
  1439. .cra_alignmask = OMAP_ALIGN_MASK,
  1440. .cra_module = THIS_MODULE,
  1441. .cra_init = omap_sham_cra_sha512_init,
  1442. .cra_exit = omap_sham_cra_exit,
  1443. }
  1444. },
  1445. };
  1446. static void omap_sham_done_task(unsigned long data)
  1447. {
  1448. struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
  1449. int err = 0;
  1450. if (!test_bit(FLAGS_BUSY, &dd->flags)) {
  1451. omap_sham_handle_queue(dd, NULL);
  1452. return;
  1453. }
  1454. if (test_bit(FLAGS_CPU, &dd->flags)) {
  1455. if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
  1456. goto finish;
  1457. } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
  1458. if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
  1459. omap_sham_update_dma_stop(dd);
  1460. if (dd->err) {
  1461. err = dd->err;
  1462. goto finish;
  1463. }
  1464. }
  1465. if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
  1466. /* hash or semi-hash ready */
  1467. clear_bit(FLAGS_DMA_READY, &dd->flags);
  1468. goto finish;
  1469. }
  1470. }
  1471. return;
  1472. finish:
  1473. dev_dbg(dd->dev, "update done: err: %d\n", err);
  1474. /* finish curent request */
  1475. omap_sham_finish_req(dd->req, err);
  1476. /* If we are not busy, process next req */
  1477. if (!test_bit(FLAGS_BUSY, &dd->flags))
  1478. omap_sham_handle_queue(dd, NULL);
  1479. }
  1480. static irqreturn_t omap_sham_irq_common(struct omap_sham_dev *dd)
  1481. {
  1482. if (!test_bit(FLAGS_BUSY, &dd->flags)) {
  1483. dev_warn(dd->dev, "Interrupt when no active requests.\n");
  1484. } else {
  1485. set_bit(FLAGS_OUTPUT_READY, &dd->flags);
  1486. tasklet_schedule(&dd->done_task);
  1487. }
  1488. return IRQ_HANDLED;
  1489. }
  1490. static irqreturn_t omap_sham_irq_omap2(int irq, void *dev_id)
  1491. {
  1492. struct omap_sham_dev *dd = dev_id;
  1493. if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
  1494. /* final -> allow device to go to power-saving mode */
  1495. omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
  1496. omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
  1497. SHA_REG_CTRL_OUTPUT_READY);
  1498. omap_sham_read(dd, SHA_REG_CTRL);
  1499. return omap_sham_irq_common(dd);
  1500. }
  1501. static irqreturn_t omap_sham_irq_omap4(int irq, void *dev_id)
  1502. {
  1503. struct omap_sham_dev *dd = dev_id;
  1504. omap_sham_write_mask(dd, SHA_REG_MASK(dd), 0, SHA_REG_MASK_IT_EN);
  1505. return omap_sham_irq_common(dd);
  1506. }
  1507. static struct omap_sham_algs_info omap_sham_algs_info_omap2[] = {
  1508. {
  1509. .algs_list = algs_sha1_md5,
  1510. .size = ARRAY_SIZE(algs_sha1_md5),
  1511. },
  1512. };
  1513. static const struct omap_sham_pdata omap_sham_pdata_omap2 = {
  1514. .algs_info = omap_sham_algs_info_omap2,
  1515. .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap2),
  1516. .flags = BIT(FLAGS_BE32_SHA1),
  1517. .digest_size = SHA1_DIGEST_SIZE,
  1518. .copy_hash = omap_sham_copy_hash_omap2,
  1519. .write_ctrl = omap_sham_write_ctrl_omap2,
  1520. .trigger = omap_sham_trigger_omap2,
  1521. .poll_irq = omap_sham_poll_irq_omap2,
  1522. .intr_hdlr = omap_sham_irq_omap2,
  1523. .idigest_ofs = 0x00,
  1524. .din_ofs = 0x1c,
  1525. .digcnt_ofs = 0x14,
  1526. .rev_ofs = 0x5c,
  1527. .mask_ofs = 0x60,
  1528. .sysstatus_ofs = 0x64,
  1529. .major_mask = 0xf0,
  1530. .major_shift = 4,
  1531. .minor_mask = 0x0f,
  1532. .minor_shift = 0,
  1533. };
  1534. #ifdef CONFIG_OF
  1535. static struct omap_sham_algs_info omap_sham_algs_info_omap4[] = {
  1536. {
  1537. .algs_list = algs_sha1_md5,
  1538. .size = ARRAY_SIZE(algs_sha1_md5),
  1539. },
  1540. {
  1541. .algs_list = algs_sha224_sha256,
  1542. .size = ARRAY_SIZE(algs_sha224_sha256),
  1543. },
  1544. };
  1545. static const struct omap_sham_pdata omap_sham_pdata_omap4 = {
  1546. .algs_info = omap_sham_algs_info_omap4,
  1547. .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap4),
  1548. .flags = BIT(FLAGS_AUTO_XOR),
  1549. .digest_size = SHA256_DIGEST_SIZE,
  1550. .copy_hash = omap_sham_copy_hash_omap4,
  1551. .write_ctrl = omap_sham_write_ctrl_omap4,
  1552. .trigger = omap_sham_trigger_omap4,
  1553. .poll_irq = omap_sham_poll_irq_omap4,
  1554. .intr_hdlr = omap_sham_irq_omap4,
  1555. .idigest_ofs = 0x020,
  1556. .odigest_ofs = 0x0,
  1557. .din_ofs = 0x080,
  1558. .digcnt_ofs = 0x040,
  1559. .rev_ofs = 0x100,
  1560. .mask_ofs = 0x110,
  1561. .sysstatus_ofs = 0x114,
  1562. .mode_ofs = 0x44,
  1563. .length_ofs = 0x48,
  1564. .major_mask = 0x0700,
  1565. .major_shift = 8,
  1566. .minor_mask = 0x003f,
  1567. .minor_shift = 0,
  1568. };
  1569. static struct omap_sham_algs_info omap_sham_algs_info_omap5[] = {
  1570. {
  1571. .algs_list = algs_sha1_md5,
  1572. .size = ARRAY_SIZE(algs_sha1_md5),
  1573. },
  1574. {
  1575. .algs_list = algs_sha224_sha256,
  1576. .size = ARRAY_SIZE(algs_sha224_sha256),
  1577. },
  1578. {
  1579. .algs_list = algs_sha384_sha512,
  1580. .size = ARRAY_SIZE(algs_sha384_sha512),
  1581. },
  1582. };
  1583. static const struct omap_sham_pdata omap_sham_pdata_omap5 = {
  1584. .algs_info = omap_sham_algs_info_omap5,
  1585. .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap5),
  1586. .flags = BIT(FLAGS_AUTO_XOR),
  1587. .digest_size = SHA512_DIGEST_SIZE,
  1588. .copy_hash = omap_sham_copy_hash_omap4,
  1589. .write_ctrl = omap_sham_write_ctrl_omap4,
  1590. .trigger = omap_sham_trigger_omap4,
  1591. .poll_irq = omap_sham_poll_irq_omap4,
  1592. .intr_hdlr = omap_sham_irq_omap4,
  1593. .idigest_ofs = 0x240,
  1594. .odigest_ofs = 0x200,
  1595. .din_ofs = 0x080,
  1596. .digcnt_ofs = 0x280,
  1597. .rev_ofs = 0x100,
  1598. .mask_ofs = 0x110,
  1599. .sysstatus_ofs = 0x114,
  1600. .mode_ofs = 0x284,
  1601. .length_ofs = 0x288,
  1602. .major_mask = 0x0700,
  1603. .major_shift = 8,
  1604. .minor_mask = 0x003f,
  1605. .minor_shift = 0,
  1606. };
  1607. static const struct of_device_id omap_sham_of_match[] = {
  1608. {
  1609. .compatible = "ti,omap2-sham",
  1610. .data = &omap_sham_pdata_omap2,
  1611. },
  1612. {
  1613. .compatible = "ti,omap3-sham",
  1614. .data = &omap_sham_pdata_omap2,
  1615. },
  1616. {
  1617. .compatible = "ti,omap4-sham",
  1618. .data = &omap_sham_pdata_omap4,
  1619. },
  1620. {
  1621. .compatible = "ti,omap5-sham",
  1622. .data = &omap_sham_pdata_omap5,
  1623. },
  1624. {},
  1625. };
  1626. MODULE_DEVICE_TABLE(of, omap_sham_of_match);
  1627. static int omap_sham_get_res_of(struct omap_sham_dev *dd,
  1628. struct device *dev, struct resource *res)
  1629. {
  1630. struct device_node *node = dev->of_node;
  1631. const struct of_device_id *match;
  1632. int err = 0;
  1633. match = of_match_device(of_match_ptr(omap_sham_of_match), dev);
  1634. if (!match) {
  1635. dev_err(dev, "no compatible OF match\n");
  1636. err = -EINVAL;
  1637. goto err;
  1638. }
  1639. err = of_address_to_resource(node, 0, res);
  1640. if (err < 0) {
  1641. dev_err(dev, "can't translate OF node address\n");
  1642. err = -EINVAL;
  1643. goto err;
  1644. }
  1645. dd->irq = irq_of_parse_and_map(node, 0);
  1646. if (!dd->irq) {
  1647. dev_err(dev, "can't translate OF irq value\n");
  1648. err = -EINVAL;
  1649. goto err;
  1650. }
  1651. dd->pdata = match->data;
  1652. err:
  1653. return err;
  1654. }
  1655. #else
  1656. static const struct of_device_id omap_sham_of_match[] = {
  1657. {},
  1658. };
  1659. static int omap_sham_get_res_of(struct omap_sham_dev *dd,
  1660. struct device *dev, struct resource *res)
  1661. {
  1662. return -EINVAL;
  1663. }
  1664. #endif
  1665. static int omap_sham_get_res_pdev(struct omap_sham_dev *dd,
  1666. struct platform_device *pdev, struct resource *res)
  1667. {
  1668. struct device *dev = &pdev->dev;
  1669. struct resource *r;
  1670. int err = 0;
  1671. /* Get the base address */
  1672. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1673. if (!r) {
  1674. dev_err(dev, "no MEM resource info\n");
  1675. err = -ENODEV;
  1676. goto err;
  1677. }
  1678. memcpy(res, r, sizeof(*res));
  1679. /* Get the IRQ */
  1680. dd->irq = platform_get_irq(pdev, 0);
  1681. if (dd->irq < 0) {
  1682. dev_err(dev, "no IRQ resource info\n");
  1683. err = dd->irq;
  1684. goto err;
  1685. }
  1686. /* Only OMAP2/3 can be non-DT */
  1687. dd->pdata = &omap_sham_pdata_omap2;
  1688. err:
  1689. return err;
  1690. }
  1691. static int omap_sham_probe(struct platform_device *pdev)
  1692. {
  1693. struct omap_sham_dev *dd;
  1694. struct device *dev = &pdev->dev;
  1695. struct resource res;
  1696. dma_cap_mask_t mask;
  1697. int err, i, j;
  1698. u32 rev;
  1699. dd = devm_kzalloc(dev, sizeof(struct omap_sham_dev), GFP_KERNEL);
  1700. if (dd == NULL) {
  1701. dev_err(dev, "unable to alloc data struct.\n");
  1702. err = -ENOMEM;
  1703. goto data_err;
  1704. }
  1705. dd->dev = dev;
  1706. platform_set_drvdata(pdev, dd);
  1707. INIT_LIST_HEAD(&dd->list);
  1708. spin_lock_init(&dd->lock);
  1709. tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
  1710. crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
  1711. err = (dev->of_node) ? omap_sham_get_res_of(dd, dev, &res) :
  1712. omap_sham_get_res_pdev(dd, pdev, &res);
  1713. if (err)
  1714. goto data_err;
  1715. dd->io_base = devm_ioremap_resource(dev, &res);
  1716. if (IS_ERR(dd->io_base)) {
  1717. err = PTR_ERR(dd->io_base);
  1718. goto data_err;
  1719. }
  1720. dd->phys_base = res.start;
  1721. err = devm_request_irq(dev, dd->irq, dd->pdata->intr_hdlr,
  1722. IRQF_TRIGGER_NONE, dev_name(dev), dd);
  1723. if (err) {
  1724. dev_err(dev, "unable to request irq %d, err = %d\n",
  1725. dd->irq, err);
  1726. goto data_err;
  1727. }
  1728. dma_cap_zero(mask);
  1729. dma_cap_set(DMA_SLAVE, mask);
  1730. dd->dma_lch = dma_request_chan(dev, "rx");
  1731. if (IS_ERR(dd->dma_lch)) {
  1732. err = PTR_ERR(dd->dma_lch);
  1733. if (err == -EPROBE_DEFER)
  1734. goto data_err;
  1735. dd->polling_mode = 1;
  1736. dev_dbg(dev, "using polling mode instead of dma\n");
  1737. }
  1738. dd->flags |= dd->pdata->flags;
  1739. pm_runtime_use_autosuspend(dev);
  1740. pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
  1741. pm_runtime_enable(dev);
  1742. pm_runtime_irq_safe(dev);
  1743. err = pm_runtime_get_sync(dev);
  1744. if (err < 0) {
  1745. dev_err(dev, "failed to get sync: %d\n", err);
  1746. goto err_pm;
  1747. }
  1748. rev = omap_sham_read(dd, SHA_REG_REV(dd));
  1749. pm_runtime_put_sync(&pdev->dev);
  1750. dev_info(dev, "hw accel on OMAP rev %u.%u\n",
  1751. (rev & dd->pdata->major_mask) >> dd->pdata->major_shift,
  1752. (rev & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
  1753. spin_lock(&sham.lock);
  1754. list_add_tail(&dd->list, &sham.dev_list);
  1755. spin_unlock(&sham.lock);
  1756. for (i = 0; i < dd->pdata->algs_info_size; i++) {
  1757. for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
  1758. struct ahash_alg *alg;
  1759. alg = &dd->pdata->algs_info[i].algs_list[j];
  1760. alg->export = omap_sham_export;
  1761. alg->import = omap_sham_import;
  1762. alg->halg.statesize = sizeof(struct omap_sham_reqctx) +
  1763. BUFLEN;
  1764. err = crypto_register_ahash(alg);
  1765. if (err)
  1766. goto err_algs;
  1767. dd->pdata->algs_info[i].registered++;
  1768. }
  1769. }
  1770. return 0;
  1771. err_algs:
  1772. for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
  1773. for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
  1774. crypto_unregister_ahash(
  1775. &dd->pdata->algs_info[i].algs_list[j]);
  1776. err_pm:
  1777. pm_runtime_disable(dev);
  1778. if (!dd->polling_mode)
  1779. dma_release_channel(dd->dma_lch);
  1780. data_err:
  1781. dev_err(dev, "initialization failed.\n");
  1782. return err;
  1783. }
  1784. static int omap_sham_remove(struct platform_device *pdev)
  1785. {
  1786. static struct omap_sham_dev *dd;
  1787. int i, j;
  1788. dd = platform_get_drvdata(pdev);
  1789. if (!dd)
  1790. return -ENODEV;
  1791. spin_lock(&sham.lock);
  1792. list_del(&dd->list);
  1793. spin_unlock(&sham.lock);
  1794. for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
  1795. for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
  1796. crypto_unregister_ahash(
  1797. &dd->pdata->algs_info[i].algs_list[j]);
  1798. tasklet_kill(&dd->done_task);
  1799. pm_runtime_disable(&pdev->dev);
  1800. if (!dd->polling_mode)
  1801. dma_release_channel(dd->dma_lch);
  1802. return 0;
  1803. }
  1804. #ifdef CONFIG_PM_SLEEP
  1805. static int omap_sham_suspend(struct device *dev)
  1806. {
  1807. pm_runtime_put_sync(dev);
  1808. return 0;
  1809. }
  1810. static int omap_sham_resume(struct device *dev)
  1811. {
  1812. int err = pm_runtime_get_sync(dev);
  1813. if (err < 0) {
  1814. dev_err(dev, "failed to get sync: %d\n", err);
  1815. return err;
  1816. }
  1817. return 0;
  1818. }
  1819. #endif
  1820. static SIMPLE_DEV_PM_OPS(omap_sham_pm_ops, omap_sham_suspend, omap_sham_resume);
  1821. static struct platform_driver omap_sham_driver = {
  1822. .probe = omap_sham_probe,
  1823. .remove = omap_sham_remove,
  1824. .driver = {
  1825. .name = "omap-sham",
  1826. .pm = &omap_sham_pm_ops,
  1827. .of_match_table = omap_sham_of_match,
  1828. },
  1829. };
  1830. module_platform_driver(omap_sham_driver);
  1831. MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
  1832. MODULE_LICENSE("GPL v2");
  1833. MODULE_AUTHOR("Dmitry Kasatkin");
  1834. MODULE_ALIAS("platform:omap-sham");