omap-sham.c 52 KB

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