omap-sham.c 51 KB

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