omap-sham.c 54 KB

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