img-hash.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014 Imagination Technologies
  4. * Authors: Will Thomas, James Hartley
  5. *
  6. * Interface structure taken from omap-sham driver
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/dmaengine.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/scatterlist.h>
  18. #include <crypto/internal/hash.h>
  19. #include <crypto/md5.h>
  20. #include <crypto/sha1.h>
  21. #include <crypto/sha2.h>
  22. #define CR_RESET 0
  23. #define CR_RESET_SET 1
  24. #define CR_RESET_UNSET 0
  25. #define CR_MESSAGE_LENGTH_H 0x4
  26. #define CR_MESSAGE_LENGTH_L 0x8
  27. #define CR_CONTROL 0xc
  28. #define CR_CONTROL_BYTE_ORDER_3210 0
  29. #define CR_CONTROL_BYTE_ORDER_0123 1
  30. #define CR_CONTROL_BYTE_ORDER_2310 2
  31. #define CR_CONTROL_BYTE_ORDER_1032 3
  32. #define CR_CONTROL_BYTE_ORDER_SHIFT 8
  33. #define CR_CONTROL_ALGO_MD5 0
  34. #define CR_CONTROL_ALGO_SHA1 1
  35. #define CR_CONTROL_ALGO_SHA224 2
  36. #define CR_CONTROL_ALGO_SHA256 3
  37. #define CR_INTSTAT 0x10
  38. #define CR_INTENAB 0x14
  39. #define CR_INTCLEAR 0x18
  40. #define CR_INT_RESULTS_AVAILABLE BIT(0)
  41. #define CR_INT_NEW_RESULTS_SET BIT(1)
  42. #define CR_INT_RESULT_READ_ERR BIT(2)
  43. #define CR_INT_MESSAGE_WRITE_ERROR BIT(3)
  44. #define CR_INT_STATUS BIT(8)
  45. #define CR_RESULT_QUEUE 0x1c
  46. #define CR_RSD0 0x40
  47. #define CR_CORE_REV 0x50
  48. #define CR_CORE_DES1 0x60
  49. #define CR_CORE_DES2 0x70
  50. #define DRIVER_FLAGS_BUSY BIT(0)
  51. #define DRIVER_FLAGS_FINAL BIT(1)
  52. #define DRIVER_FLAGS_DMA_ACTIVE BIT(2)
  53. #define DRIVER_FLAGS_OUTPUT_READY BIT(3)
  54. #define DRIVER_FLAGS_INIT BIT(4)
  55. #define DRIVER_FLAGS_CPU BIT(5)
  56. #define DRIVER_FLAGS_DMA_READY BIT(6)
  57. #define DRIVER_FLAGS_ERROR BIT(7)
  58. #define DRIVER_FLAGS_SG BIT(8)
  59. #define DRIVER_FLAGS_SHA1 BIT(18)
  60. #define DRIVER_FLAGS_SHA224 BIT(19)
  61. #define DRIVER_FLAGS_SHA256 BIT(20)
  62. #define DRIVER_FLAGS_MD5 BIT(21)
  63. #define IMG_HASH_QUEUE_LENGTH 20
  64. #define IMG_HASH_DMA_BURST 4
  65. #define IMG_HASH_DMA_THRESHOLD 64
  66. #ifdef __LITTLE_ENDIAN
  67. #define IMG_HASH_BYTE_ORDER CR_CONTROL_BYTE_ORDER_3210
  68. #else
  69. #define IMG_HASH_BYTE_ORDER CR_CONTROL_BYTE_ORDER_0123
  70. #endif
  71. struct img_hash_dev;
  72. struct img_hash_request_ctx {
  73. struct img_hash_dev *hdev;
  74. u8 digest[SHA256_DIGEST_SIZE] __aligned(sizeof(u32));
  75. unsigned long flags;
  76. size_t digsize;
  77. dma_addr_t dma_addr;
  78. size_t dma_ct;
  79. /* sg root */
  80. struct scatterlist *sgfirst;
  81. /* walk state */
  82. struct scatterlist *sg;
  83. size_t nents;
  84. size_t offset;
  85. unsigned int total;
  86. size_t sent;
  87. unsigned long op;
  88. size_t bufcnt;
  89. struct ahash_request fallback_req;
  90. /* Zero length buffer must remain last member of struct */
  91. u8 buffer[] __aligned(sizeof(u32));
  92. };
  93. struct img_hash_ctx {
  94. struct img_hash_dev *hdev;
  95. unsigned long flags;
  96. struct crypto_ahash *fallback;
  97. };
  98. struct img_hash_dev {
  99. struct list_head list;
  100. struct device *dev;
  101. struct clk *hash_clk;
  102. struct clk *sys_clk;
  103. void __iomem *io_base;
  104. phys_addr_t bus_addr;
  105. void __iomem *cpu_addr;
  106. spinlock_t lock;
  107. int err;
  108. struct tasklet_struct done_task;
  109. struct tasklet_struct dma_task;
  110. unsigned long flags;
  111. struct crypto_queue queue;
  112. struct ahash_request *req;
  113. struct dma_chan *dma_lch;
  114. };
  115. struct img_hash_drv {
  116. struct list_head dev_list;
  117. spinlock_t lock;
  118. };
  119. static struct img_hash_drv img_hash = {
  120. .dev_list = LIST_HEAD_INIT(img_hash.dev_list),
  121. .lock = __SPIN_LOCK_UNLOCKED(img_hash.lock),
  122. };
  123. static inline u32 img_hash_read(struct img_hash_dev *hdev, u32 offset)
  124. {
  125. return readl_relaxed(hdev->io_base + offset);
  126. }
  127. static inline void img_hash_write(struct img_hash_dev *hdev,
  128. u32 offset, u32 value)
  129. {
  130. writel_relaxed(value, hdev->io_base + offset);
  131. }
  132. static inline __be32 img_hash_read_result_queue(struct img_hash_dev *hdev)
  133. {
  134. return cpu_to_be32(img_hash_read(hdev, CR_RESULT_QUEUE));
  135. }
  136. static void img_hash_start(struct img_hash_dev *hdev, bool dma)
  137. {
  138. struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
  139. u32 cr = IMG_HASH_BYTE_ORDER << CR_CONTROL_BYTE_ORDER_SHIFT;
  140. if (ctx->flags & DRIVER_FLAGS_MD5)
  141. cr |= CR_CONTROL_ALGO_MD5;
  142. else if (ctx->flags & DRIVER_FLAGS_SHA1)
  143. cr |= CR_CONTROL_ALGO_SHA1;
  144. else if (ctx->flags & DRIVER_FLAGS_SHA224)
  145. cr |= CR_CONTROL_ALGO_SHA224;
  146. else if (ctx->flags & DRIVER_FLAGS_SHA256)
  147. cr |= CR_CONTROL_ALGO_SHA256;
  148. dev_dbg(hdev->dev, "Starting hash process\n");
  149. img_hash_write(hdev, CR_CONTROL, cr);
  150. /*
  151. * The hardware block requires two cycles between writing the control
  152. * register and writing the first word of data in non DMA mode, to
  153. * ensure the first data write is not grouped in burst with the control
  154. * register write a read is issued to 'flush' the bus.
  155. */
  156. if (!dma)
  157. img_hash_read(hdev, CR_CONTROL);
  158. }
  159. static int img_hash_xmit_cpu(struct img_hash_dev *hdev, const u8 *buf,
  160. size_t length, int final)
  161. {
  162. u32 count, len32;
  163. const u32 *buffer = (const u32 *)buf;
  164. dev_dbg(hdev->dev, "xmit_cpu: length: %zu bytes\n", length);
  165. if (final)
  166. hdev->flags |= DRIVER_FLAGS_FINAL;
  167. len32 = DIV_ROUND_UP(length, sizeof(u32));
  168. for (count = 0; count < len32; count++)
  169. writel_relaxed(buffer[count], hdev->cpu_addr);
  170. return -EINPROGRESS;
  171. }
  172. static void img_hash_dma_callback(void *data)
  173. {
  174. struct img_hash_dev *hdev = data;
  175. struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
  176. if (ctx->bufcnt) {
  177. img_hash_xmit_cpu(hdev, ctx->buffer, ctx->bufcnt, 0);
  178. ctx->bufcnt = 0;
  179. }
  180. if (ctx->sg)
  181. tasklet_schedule(&hdev->dma_task);
  182. }
  183. static int img_hash_xmit_dma(struct img_hash_dev *hdev, struct scatterlist *sg)
  184. {
  185. struct dma_async_tx_descriptor *desc;
  186. struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
  187. ctx->dma_ct = dma_map_sg(hdev->dev, sg, 1, DMA_TO_DEVICE);
  188. if (ctx->dma_ct == 0) {
  189. dev_err(hdev->dev, "Invalid DMA sg\n");
  190. hdev->err = -EINVAL;
  191. return -EINVAL;
  192. }
  193. desc = dmaengine_prep_slave_sg(hdev->dma_lch,
  194. sg,
  195. ctx->dma_ct,
  196. DMA_MEM_TO_DEV,
  197. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  198. if (!desc) {
  199. dev_err(hdev->dev, "Null DMA descriptor\n");
  200. hdev->err = -EINVAL;
  201. dma_unmap_sg(hdev->dev, sg, 1, DMA_TO_DEVICE);
  202. return -EINVAL;
  203. }
  204. desc->callback = img_hash_dma_callback;
  205. desc->callback_param = hdev;
  206. dmaengine_submit(desc);
  207. dma_async_issue_pending(hdev->dma_lch);
  208. return 0;
  209. }
  210. static int img_hash_write_via_cpu(struct img_hash_dev *hdev)
  211. {
  212. struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
  213. ctx->bufcnt = sg_copy_to_buffer(hdev->req->src, sg_nents(ctx->sg),
  214. ctx->buffer, hdev->req->nbytes);
  215. ctx->total = hdev->req->nbytes;
  216. ctx->bufcnt = 0;
  217. hdev->flags |= (DRIVER_FLAGS_CPU | DRIVER_FLAGS_FINAL);
  218. img_hash_start(hdev, false);
  219. return img_hash_xmit_cpu(hdev, ctx->buffer, ctx->total, 1);
  220. }
  221. static int img_hash_finish(struct ahash_request *req)
  222. {
  223. struct img_hash_request_ctx *ctx = ahash_request_ctx(req);
  224. if (!req->result)
  225. return -EINVAL;
  226. memcpy(req->result, ctx->digest, ctx->digsize);
  227. return 0;
  228. }
  229. static void img_hash_copy_hash(struct ahash_request *req)
  230. {
  231. struct img_hash_request_ctx *ctx = ahash_request_ctx(req);
  232. __be32 *hash = (__be32 *)ctx->digest;
  233. int i;
  234. for (i = (ctx->digsize / sizeof(*hash)) - 1; i >= 0; i--)
  235. hash[i] = img_hash_read_result_queue(ctx->hdev);
  236. }
  237. static void img_hash_finish_req(struct ahash_request *req, int err)
  238. {
  239. struct img_hash_request_ctx *ctx = ahash_request_ctx(req);
  240. struct img_hash_dev *hdev = ctx->hdev;
  241. if (!err) {
  242. img_hash_copy_hash(req);
  243. if (DRIVER_FLAGS_FINAL & hdev->flags)
  244. err = img_hash_finish(req);
  245. } else {
  246. dev_warn(hdev->dev, "Hash failed with error %d\n", err);
  247. ctx->flags |= DRIVER_FLAGS_ERROR;
  248. }
  249. hdev->flags &= ~(DRIVER_FLAGS_DMA_READY | DRIVER_FLAGS_OUTPUT_READY |
  250. DRIVER_FLAGS_CPU | DRIVER_FLAGS_BUSY | DRIVER_FLAGS_FINAL);
  251. if (req->base.complete)
  252. ahash_request_complete(req, err);
  253. }
  254. static int img_hash_write_via_dma(struct img_hash_dev *hdev)
  255. {
  256. struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
  257. img_hash_start(hdev, true);
  258. dev_dbg(hdev->dev, "xmit dma size: %d\n", ctx->total);
  259. if (!ctx->total)
  260. hdev->flags |= DRIVER_FLAGS_FINAL;
  261. hdev->flags |= DRIVER_FLAGS_DMA_ACTIVE | DRIVER_FLAGS_FINAL;
  262. tasklet_schedule(&hdev->dma_task);
  263. return -EINPROGRESS;
  264. }
  265. static int img_hash_dma_init(struct img_hash_dev *hdev)
  266. {
  267. struct dma_slave_config dma_conf;
  268. int err;
  269. hdev->dma_lch = dma_request_chan(hdev->dev, "tx");
  270. if (IS_ERR(hdev->dma_lch)) {
  271. dev_err(hdev->dev, "Couldn't acquire a slave DMA channel.\n");
  272. return PTR_ERR(hdev->dma_lch);
  273. }
  274. dma_conf.direction = DMA_MEM_TO_DEV;
  275. dma_conf.dst_addr = hdev->bus_addr;
  276. dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  277. dma_conf.dst_maxburst = IMG_HASH_DMA_BURST;
  278. dma_conf.device_fc = false;
  279. err = dmaengine_slave_config(hdev->dma_lch, &dma_conf);
  280. if (err) {
  281. dev_err(hdev->dev, "Couldn't configure DMA slave.\n");
  282. dma_release_channel(hdev->dma_lch);
  283. return err;
  284. }
  285. return 0;
  286. }
  287. static void img_hash_dma_task(unsigned long d)
  288. {
  289. struct img_hash_dev *hdev = (struct img_hash_dev *)d;
  290. struct img_hash_request_ctx *ctx;
  291. u8 *addr;
  292. size_t nbytes, bleft, wsend, len, tbc;
  293. struct scatterlist tsg;
  294. if (!hdev->req)
  295. return;
  296. ctx = ahash_request_ctx(hdev->req);
  297. if (!ctx->sg)
  298. return;
  299. addr = sg_virt(ctx->sg);
  300. nbytes = ctx->sg->length - ctx->offset;
  301. /*
  302. * The hash accelerator does not support a data valid mask. This means
  303. * that if each dma (i.e. per page) is not a multiple of 4 bytes, the
  304. * padding bytes in the last word written by that dma would erroneously
  305. * be included in the hash. To avoid this we round down the transfer,
  306. * and add the excess to the start of the next dma. It does not matter
  307. * that the final dma may not be a multiple of 4 bytes as the hashing
  308. * block is programmed to accept the correct number of bytes.
  309. */
  310. bleft = nbytes % 4;
  311. wsend = (nbytes / 4);
  312. if (wsend) {
  313. sg_init_one(&tsg, addr + ctx->offset, wsend * 4);
  314. if (img_hash_xmit_dma(hdev, &tsg)) {
  315. dev_err(hdev->dev, "DMA failed, falling back to CPU");
  316. ctx->flags |= DRIVER_FLAGS_CPU;
  317. hdev->err = 0;
  318. img_hash_xmit_cpu(hdev, addr + ctx->offset,
  319. wsend * 4, 0);
  320. ctx->sent += wsend * 4;
  321. wsend = 0;
  322. } else {
  323. ctx->sent += wsend * 4;
  324. }
  325. }
  326. if (bleft) {
  327. ctx->bufcnt = sg_pcopy_to_buffer(ctx->sgfirst, ctx->nents,
  328. ctx->buffer, bleft, ctx->sent);
  329. tbc = 0;
  330. ctx->sg = sg_next(ctx->sg);
  331. while (ctx->sg && (ctx->bufcnt < 4)) {
  332. len = ctx->sg->length;
  333. if (likely(len > (4 - ctx->bufcnt)))
  334. len = 4 - ctx->bufcnt;
  335. tbc = sg_pcopy_to_buffer(ctx->sgfirst, ctx->nents,
  336. ctx->buffer + ctx->bufcnt, len,
  337. ctx->sent + ctx->bufcnt);
  338. ctx->bufcnt += tbc;
  339. if (tbc >= ctx->sg->length) {
  340. ctx->sg = sg_next(ctx->sg);
  341. tbc = 0;
  342. }
  343. }
  344. ctx->sent += ctx->bufcnt;
  345. ctx->offset = tbc;
  346. if (!wsend)
  347. img_hash_dma_callback(hdev);
  348. } else {
  349. ctx->offset = 0;
  350. ctx->sg = sg_next(ctx->sg);
  351. }
  352. }
  353. static int img_hash_write_via_dma_stop(struct img_hash_dev *hdev)
  354. {
  355. struct img_hash_request_ctx *ctx = ahash_request_ctx(hdev->req);
  356. if (ctx->flags & DRIVER_FLAGS_SG)
  357. dma_unmap_sg(hdev->dev, ctx->sg, 1, DMA_TO_DEVICE);
  358. return 0;
  359. }
  360. static int img_hash_process_data(struct img_hash_dev *hdev)
  361. {
  362. struct ahash_request *req = hdev->req;
  363. struct img_hash_request_ctx *ctx = ahash_request_ctx(req);
  364. int err = 0;
  365. ctx->bufcnt = 0;
  366. if (req->nbytes >= IMG_HASH_DMA_THRESHOLD) {
  367. dev_dbg(hdev->dev, "process data request(%d bytes) using DMA\n",
  368. req->nbytes);
  369. err = img_hash_write_via_dma(hdev);
  370. } else {
  371. dev_dbg(hdev->dev, "process data request(%d bytes) using CPU\n",
  372. req->nbytes);
  373. err = img_hash_write_via_cpu(hdev);
  374. }
  375. return err;
  376. }
  377. static int img_hash_hw_init(struct img_hash_dev *hdev)
  378. {
  379. unsigned long long nbits;
  380. u32 u, l;
  381. img_hash_write(hdev, CR_RESET, CR_RESET_SET);
  382. img_hash_write(hdev, CR_RESET, CR_RESET_UNSET);
  383. img_hash_write(hdev, CR_INTENAB, CR_INT_NEW_RESULTS_SET);
  384. nbits = (u64)hdev->req->nbytes << 3;
  385. u = nbits >> 32;
  386. l = nbits;
  387. img_hash_write(hdev, CR_MESSAGE_LENGTH_H, u);
  388. img_hash_write(hdev, CR_MESSAGE_LENGTH_L, l);
  389. if (!(DRIVER_FLAGS_INIT & hdev->flags)) {
  390. hdev->flags |= DRIVER_FLAGS_INIT;
  391. hdev->err = 0;
  392. }
  393. dev_dbg(hdev->dev, "hw initialized, nbits: %llx\n", nbits);
  394. return 0;
  395. }
  396. static int img_hash_init(struct ahash_request *req)
  397. {
  398. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  399. struct img_hash_request_ctx *rctx = ahash_request_ctx(req);
  400. struct img_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  401. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback);
  402. ahash_request_set_callback(&rctx->fallback_req,
  403. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
  404. req->base.complete, req->base.data);
  405. return crypto_ahash_init(&rctx->fallback_req);
  406. }
  407. static int img_hash_handle_queue(struct img_hash_dev *hdev,
  408. struct ahash_request *req)
  409. {
  410. struct crypto_async_request *async_req, *backlog;
  411. struct img_hash_request_ctx *ctx;
  412. unsigned long flags;
  413. int err = 0, res = 0;
  414. spin_lock_irqsave(&hdev->lock, flags);
  415. if (req)
  416. res = ahash_enqueue_request(&hdev->queue, req);
  417. if (DRIVER_FLAGS_BUSY & hdev->flags) {
  418. spin_unlock_irqrestore(&hdev->lock, flags);
  419. return res;
  420. }
  421. backlog = crypto_get_backlog(&hdev->queue);
  422. async_req = crypto_dequeue_request(&hdev->queue);
  423. if (async_req)
  424. hdev->flags |= DRIVER_FLAGS_BUSY;
  425. spin_unlock_irqrestore(&hdev->lock, flags);
  426. if (!async_req)
  427. return res;
  428. if (backlog)
  429. crypto_request_complete(backlog, -EINPROGRESS);
  430. req = ahash_request_cast(async_req);
  431. hdev->req = req;
  432. ctx = ahash_request_ctx(req);
  433. dev_info(hdev->dev, "processing req, op: %lu, bytes: %d\n",
  434. ctx->op, req->nbytes);
  435. err = img_hash_hw_init(hdev);
  436. if (!err)
  437. err = img_hash_process_data(hdev);
  438. if (err != -EINPROGRESS) {
  439. /* done_task will not finish so do it here */
  440. img_hash_finish_req(req, err);
  441. }
  442. return res;
  443. }
  444. static int img_hash_update(struct ahash_request *req)
  445. {
  446. struct img_hash_request_ctx *rctx = ahash_request_ctx(req);
  447. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  448. struct img_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  449. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback);
  450. ahash_request_set_callback(&rctx->fallback_req,
  451. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
  452. req->base.complete, req->base.data);
  453. ahash_request_set_crypt(&rctx->fallback_req, req->src, NULL, req->nbytes);
  454. return crypto_ahash_update(&rctx->fallback_req);
  455. }
  456. static int img_hash_final(struct ahash_request *req)
  457. {
  458. struct img_hash_request_ctx *rctx = ahash_request_ctx(req);
  459. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  460. struct img_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  461. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback);
  462. ahash_request_set_callback(&rctx->fallback_req,
  463. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
  464. req->base.complete, req->base.data);
  465. ahash_request_set_crypt(&rctx->fallback_req, NULL, req->result, 0);
  466. return crypto_ahash_final(&rctx->fallback_req);
  467. }
  468. static int img_hash_finup(struct ahash_request *req)
  469. {
  470. struct img_hash_request_ctx *rctx = ahash_request_ctx(req);
  471. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  472. struct img_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  473. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback);
  474. ahash_request_set_callback(&rctx->fallback_req,
  475. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
  476. req->base.complete, req->base.data);
  477. ahash_request_set_crypt(&rctx->fallback_req, req->src, req->result,
  478. req->nbytes);
  479. return crypto_ahash_finup(&rctx->fallback_req);
  480. }
  481. static int img_hash_import(struct ahash_request *req, const void *in)
  482. {
  483. struct img_hash_request_ctx *rctx = ahash_request_ctx(req);
  484. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  485. struct img_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  486. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback);
  487. ahash_request_set_callback(&rctx->fallback_req,
  488. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
  489. req->base.complete, req->base.data);
  490. return crypto_ahash_import(&rctx->fallback_req, in);
  491. }
  492. static int img_hash_export(struct ahash_request *req, void *out)
  493. {
  494. struct img_hash_request_ctx *rctx = ahash_request_ctx(req);
  495. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  496. struct img_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  497. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback);
  498. ahash_request_set_callback(&rctx->fallback_req,
  499. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
  500. req->base.complete, req->base.data);
  501. return crypto_ahash_export(&rctx->fallback_req, out);
  502. }
  503. static int img_hash_digest(struct ahash_request *req)
  504. {
  505. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  506. struct img_hash_ctx *tctx = crypto_ahash_ctx(tfm);
  507. struct img_hash_request_ctx *ctx = ahash_request_ctx(req);
  508. struct img_hash_dev *hdev = NULL;
  509. struct img_hash_dev *tmp;
  510. int err;
  511. spin_lock(&img_hash.lock);
  512. if (!tctx->hdev) {
  513. list_for_each_entry(tmp, &img_hash.dev_list, list) {
  514. hdev = tmp;
  515. break;
  516. }
  517. tctx->hdev = hdev;
  518. } else {
  519. hdev = tctx->hdev;
  520. }
  521. spin_unlock(&img_hash.lock);
  522. ctx->hdev = hdev;
  523. ctx->flags = 0;
  524. ctx->digsize = crypto_ahash_digestsize(tfm);
  525. switch (ctx->digsize) {
  526. case SHA1_DIGEST_SIZE:
  527. ctx->flags |= DRIVER_FLAGS_SHA1;
  528. break;
  529. case SHA256_DIGEST_SIZE:
  530. ctx->flags |= DRIVER_FLAGS_SHA256;
  531. break;
  532. case SHA224_DIGEST_SIZE:
  533. ctx->flags |= DRIVER_FLAGS_SHA224;
  534. break;
  535. case MD5_DIGEST_SIZE:
  536. ctx->flags |= DRIVER_FLAGS_MD5;
  537. break;
  538. default:
  539. return -EINVAL;
  540. }
  541. ctx->bufcnt = 0;
  542. ctx->offset = 0;
  543. ctx->sent = 0;
  544. ctx->total = req->nbytes;
  545. ctx->sg = req->src;
  546. ctx->sgfirst = req->src;
  547. ctx->nents = sg_nents(ctx->sg);
  548. err = img_hash_handle_queue(tctx->hdev, req);
  549. return err;
  550. }
  551. static int img_hash_cra_init(struct crypto_tfm *tfm, const char *alg_name)
  552. {
  553. struct img_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  554. ctx->fallback = crypto_alloc_ahash(alg_name, 0,
  555. CRYPTO_ALG_NEED_FALLBACK);
  556. if (IS_ERR(ctx->fallback)) {
  557. pr_err("img_hash: Could not load fallback driver.\n");
  558. return PTR_ERR(ctx->fallback);
  559. }
  560. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  561. sizeof(struct img_hash_request_ctx) +
  562. crypto_ahash_reqsize(ctx->fallback) +
  563. IMG_HASH_DMA_THRESHOLD);
  564. return 0;
  565. }
  566. static int img_hash_cra_md5_init(struct crypto_tfm *tfm)
  567. {
  568. return img_hash_cra_init(tfm, "md5-lib");
  569. }
  570. static int img_hash_cra_sha1_init(struct crypto_tfm *tfm)
  571. {
  572. return img_hash_cra_init(tfm, "sha1-lib");
  573. }
  574. static int img_hash_cra_sha224_init(struct crypto_tfm *tfm)
  575. {
  576. return img_hash_cra_init(tfm, "sha224-lib");
  577. }
  578. static int img_hash_cra_sha256_init(struct crypto_tfm *tfm)
  579. {
  580. return img_hash_cra_init(tfm, "sha256-lib");
  581. }
  582. static void img_hash_cra_exit(struct crypto_tfm *tfm)
  583. {
  584. struct img_hash_ctx *tctx = crypto_tfm_ctx(tfm);
  585. crypto_free_ahash(tctx->fallback);
  586. }
  587. static irqreturn_t img_irq_handler(int irq, void *dev_id)
  588. {
  589. struct img_hash_dev *hdev = dev_id;
  590. u32 reg;
  591. reg = img_hash_read(hdev, CR_INTSTAT);
  592. img_hash_write(hdev, CR_INTCLEAR, reg);
  593. if (reg & CR_INT_NEW_RESULTS_SET) {
  594. dev_dbg(hdev->dev, "IRQ CR_INT_NEW_RESULTS_SET\n");
  595. if (DRIVER_FLAGS_BUSY & hdev->flags) {
  596. hdev->flags |= DRIVER_FLAGS_OUTPUT_READY;
  597. if (!(DRIVER_FLAGS_CPU & hdev->flags))
  598. hdev->flags |= DRIVER_FLAGS_DMA_READY;
  599. tasklet_schedule(&hdev->done_task);
  600. } else {
  601. dev_warn(hdev->dev,
  602. "HASH interrupt when no active requests.\n");
  603. }
  604. } else if (reg & CR_INT_RESULTS_AVAILABLE) {
  605. dev_warn(hdev->dev,
  606. "IRQ triggered before the hash had completed\n");
  607. } else if (reg & CR_INT_RESULT_READ_ERR) {
  608. dev_warn(hdev->dev,
  609. "Attempt to read from an empty result queue\n");
  610. } else if (reg & CR_INT_MESSAGE_WRITE_ERROR) {
  611. dev_warn(hdev->dev,
  612. "Data written before the hardware was configured\n");
  613. }
  614. return IRQ_HANDLED;
  615. }
  616. static struct ahash_alg img_algs[] = {
  617. {
  618. .init = img_hash_init,
  619. .update = img_hash_update,
  620. .final = img_hash_final,
  621. .finup = img_hash_finup,
  622. .export = img_hash_export,
  623. .import = img_hash_import,
  624. .digest = img_hash_digest,
  625. .halg = {
  626. .digestsize = MD5_DIGEST_SIZE,
  627. .statesize = sizeof(struct md5_state),
  628. .base = {
  629. .cra_name = "md5",
  630. .cra_driver_name = "img-md5",
  631. .cra_priority = 300,
  632. .cra_flags =
  633. CRYPTO_ALG_ASYNC |
  634. CRYPTO_ALG_NEED_FALLBACK,
  635. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  636. .cra_ctxsize = sizeof(struct img_hash_ctx),
  637. .cra_init = img_hash_cra_md5_init,
  638. .cra_exit = img_hash_cra_exit,
  639. .cra_module = THIS_MODULE,
  640. }
  641. }
  642. },
  643. {
  644. .init = img_hash_init,
  645. .update = img_hash_update,
  646. .final = img_hash_final,
  647. .finup = img_hash_finup,
  648. .export = img_hash_export,
  649. .import = img_hash_import,
  650. .digest = img_hash_digest,
  651. .halg = {
  652. .digestsize = SHA1_DIGEST_SIZE,
  653. .statesize = sizeof(struct sha1_state),
  654. .base = {
  655. .cra_name = "sha1",
  656. .cra_driver_name = "img-sha1",
  657. .cra_priority = 300,
  658. .cra_flags =
  659. CRYPTO_ALG_ASYNC |
  660. CRYPTO_ALG_NEED_FALLBACK,
  661. .cra_blocksize = SHA1_BLOCK_SIZE,
  662. .cra_ctxsize = sizeof(struct img_hash_ctx),
  663. .cra_init = img_hash_cra_sha1_init,
  664. .cra_exit = img_hash_cra_exit,
  665. .cra_module = THIS_MODULE,
  666. }
  667. }
  668. },
  669. {
  670. .init = img_hash_init,
  671. .update = img_hash_update,
  672. .final = img_hash_final,
  673. .finup = img_hash_finup,
  674. .export = img_hash_export,
  675. .import = img_hash_import,
  676. .digest = img_hash_digest,
  677. .halg = {
  678. .digestsize = SHA224_DIGEST_SIZE,
  679. .statesize = sizeof(struct sha256_state),
  680. .base = {
  681. .cra_name = "sha224",
  682. .cra_driver_name = "img-sha224",
  683. .cra_priority = 300,
  684. .cra_flags =
  685. CRYPTO_ALG_ASYNC |
  686. CRYPTO_ALG_NEED_FALLBACK,
  687. .cra_blocksize = SHA224_BLOCK_SIZE,
  688. .cra_ctxsize = sizeof(struct img_hash_ctx),
  689. .cra_init = img_hash_cra_sha224_init,
  690. .cra_exit = img_hash_cra_exit,
  691. .cra_module = THIS_MODULE,
  692. }
  693. }
  694. },
  695. {
  696. .init = img_hash_init,
  697. .update = img_hash_update,
  698. .final = img_hash_final,
  699. .finup = img_hash_finup,
  700. .export = img_hash_export,
  701. .import = img_hash_import,
  702. .digest = img_hash_digest,
  703. .halg = {
  704. .digestsize = SHA256_DIGEST_SIZE,
  705. .statesize = sizeof(struct sha256_state),
  706. .base = {
  707. .cra_name = "sha256",
  708. .cra_driver_name = "img-sha256",
  709. .cra_priority = 300,
  710. .cra_flags =
  711. CRYPTO_ALG_ASYNC |
  712. CRYPTO_ALG_NEED_FALLBACK,
  713. .cra_blocksize = SHA256_BLOCK_SIZE,
  714. .cra_ctxsize = sizeof(struct img_hash_ctx),
  715. .cra_init = img_hash_cra_sha256_init,
  716. .cra_exit = img_hash_cra_exit,
  717. .cra_module = THIS_MODULE,
  718. }
  719. }
  720. }
  721. };
  722. static int img_register_algs(struct img_hash_dev *hdev)
  723. {
  724. int i, err;
  725. for (i = 0; i < ARRAY_SIZE(img_algs); i++) {
  726. err = crypto_register_ahash(&img_algs[i]);
  727. if (err) {
  728. crypto_unregister_ahashes(img_algs, i);
  729. return err;
  730. }
  731. }
  732. return 0;
  733. }
  734. static void img_unregister_algs(struct img_hash_dev *hdev)
  735. {
  736. crypto_unregister_ahashes(img_algs, ARRAY_SIZE(img_algs));
  737. }
  738. static void img_hash_done_task(unsigned long data)
  739. {
  740. struct img_hash_dev *hdev = (struct img_hash_dev *)data;
  741. int err = 0;
  742. if (hdev->err == -EINVAL) {
  743. err = hdev->err;
  744. goto finish;
  745. }
  746. if (!(DRIVER_FLAGS_BUSY & hdev->flags)) {
  747. img_hash_handle_queue(hdev, NULL);
  748. return;
  749. }
  750. if (DRIVER_FLAGS_CPU & hdev->flags) {
  751. if (DRIVER_FLAGS_OUTPUT_READY & hdev->flags) {
  752. hdev->flags &= ~DRIVER_FLAGS_OUTPUT_READY;
  753. goto finish;
  754. }
  755. } else if (DRIVER_FLAGS_DMA_READY & hdev->flags) {
  756. if (DRIVER_FLAGS_DMA_ACTIVE & hdev->flags) {
  757. hdev->flags &= ~DRIVER_FLAGS_DMA_ACTIVE;
  758. img_hash_write_via_dma_stop(hdev);
  759. if (hdev->err) {
  760. err = hdev->err;
  761. goto finish;
  762. }
  763. }
  764. if (DRIVER_FLAGS_OUTPUT_READY & hdev->flags) {
  765. hdev->flags &= ~(DRIVER_FLAGS_DMA_READY |
  766. DRIVER_FLAGS_OUTPUT_READY);
  767. goto finish;
  768. }
  769. }
  770. return;
  771. finish:
  772. img_hash_finish_req(hdev->req, err);
  773. }
  774. static const struct of_device_id img_hash_match[] __maybe_unused = {
  775. { .compatible = "img,hash-accelerator" },
  776. {}
  777. };
  778. MODULE_DEVICE_TABLE(of, img_hash_match);
  779. static int img_hash_probe(struct platform_device *pdev)
  780. {
  781. struct img_hash_dev *hdev;
  782. struct device *dev = &pdev->dev;
  783. struct resource *hash_res;
  784. int irq;
  785. int err;
  786. hdev = devm_kzalloc(dev, sizeof(*hdev), GFP_KERNEL);
  787. if (hdev == NULL)
  788. return -ENOMEM;
  789. spin_lock_init(&hdev->lock);
  790. hdev->dev = dev;
  791. platform_set_drvdata(pdev, hdev);
  792. INIT_LIST_HEAD(&hdev->list);
  793. tasklet_init(&hdev->done_task, img_hash_done_task, (unsigned long)hdev);
  794. tasklet_init(&hdev->dma_task, img_hash_dma_task, (unsigned long)hdev);
  795. crypto_init_queue(&hdev->queue, IMG_HASH_QUEUE_LENGTH);
  796. /* Register bank */
  797. hdev->io_base = devm_platform_ioremap_resource(pdev, 0);
  798. if (IS_ERR(hdev->io_base)) {
  799. err = PTR_ERR(hdev->io_base);
  800. goto res_err;
  801. }
  802. /* Write port (DMA or CPU) */
  803. hdev->cpu_addr = devm_platform_get_and_ioremap_resource(pdev, 1, &hash_res);
  804. if (IS_ERR(hdev->cpu_addr)) {
  805. err = PTR_ERR(hdev->cpu_addr);
  806. goto res_err;
  807. }
  808. hdev->bus_addr = hash_res->start;
  809. irq = platform_get_irq(pdev, 0);
  810. if (irq < 0) {
  811. err = irq;
  812. goto res_err;
  813. }
  814. err = devm_request_irq(dev, irq, img_irq_handler, 0,
  815. dev_name(dev), hdev);
  816. if (err) {
  817. dev_err(dev, "unable to request irq\n");
  818. goto res_err;
  819. }
  820. dev_dbg(dev, "using IRQ channel %d\n", irq);
  821. hdev->hash_clk = devm_clk_get_enabled(&pdev->dev, "hash");
  822. if (IS_ERR(hdev->hash_clk)) {
  823. dev_err(dev, "clock initialization failed.\n");
  824. err = PTR_ERR(hdev->hash_clk);
  825. goto res_err;
  826. }
  827. hdev->sys_clk = devm_clk_get_enabled(&pdev->dev, "sys");
  828. if (IS_ERR(hdev->sys_clk)) {
  829. dev_err(dev, "clock initialization failed.\n");
  830. err = PTR_ERR(hdev->sys_clk);
  831. goto res_err;
  832. }
  833. err = img_hash_dma_init(hdev);
  834. if (err)
  835. goto res_err;
  836. dev_dbg(dev, "using %s for DMA transfers\n",
  837. dma_chan_name(hdev->dma_lch));
  838. spin_lock(&img_hash.lock);
  839. list_add_tail(&hdev->list, &img_hash.dev_list);
  840. spin_unlock(&img_hash.lock);
  841. err = img_register_algs(hdev);
  842. if (err)
  843. goto err_algs;
  844. dev_info(dev, "Img MD5/SHA1/SHA224/SHA256 Hardware accelerator initialized\n");
  845. return 0;
  846. err_algs:
  847. spin_lock(&img_hash.lock);
  848. list_del(&hdev->list);
  849. spin_unlock(&img_hash.lock);
  850. dma_release_channel(hdev->dma_lch);
  851. res_err:
  852. tasklet_kill(&hdev->done_task);
  853. tasklet_kill(&hdev->dma_task);
  854. return err;
  855. }
  856. static void img_hash_remove(struct platform_device *pdev)
  857. {
  858. struct img_hash_dev *hdev;
  859. hdev = platform_get_drvdata(pdev);
  860. spin_lock(&img_hash.lock);
  861. list_del(&hdev->list);
  862. spin_unlock(&img_hash.lock);
  863. img_unregister_algs(hdev);
  864. tasklet_kill(&hdev->done_task);
  865. tasklet_kill(&hdev->dma_task);
  866. dma_release_channel(hdev->dma_lch);
  867. }
  868. #ifdef CONFIG_PM_SLEEP
  869. static int img_hash_suspend(struct device *dev)
  870. {
  871. struct img_hash_dev *hdev = dev_get_drvdata(dev);
  872. clk_disable_unprepare(hdev->hash_clk);
  873. clk_disable_unprepare(hdev->sys_clk);
  874. return 0;
  875. }
  876. static int img_hash_resume(struct device *dev)
  877. {
  878. struct img_hash_dev *hdev = dev_get_drvdata(dev);
  879. int ret;
  880. ret = clk_prepare_enable(hdev->hash_clk);
  881. if (ret)
  882. return ret;
  883. ret = clk_prepare_enable(hdev->sys_clk);
  884. if (ret) {
  885. clk_disable_unprepare(hdev->hash_clk);
  886. return ret;
  887. }
  888. return 0;
  889. }
  890. #endif /* CONFIG_PM_SLEEP */
  891. static const struct dev_pm_ops img_hash_pm_ops = {
  892. SET_SYSTEM_SLEEP_PM_OPS(img_hash_suspend, img_hash_resume)
  893. };
  894. static struct platform_driver img_hash_driver = {
  895. .probe = img_hash_probe,
  896. .remove = img_hash_remove,
  897. .driver = {
  898. .name = "img-hash-accelerator",
  899. .pm = &img_hash_pm_ops,
  900. .of_match_table = img_hash_match,
  901. }
  902. };
  903. module_platform_driver(img_hash_driver);
  904. MODULE_LICENSE("GPL v2");
  905. MODULE_DESCRIPTION("Imgtec SHA1/224/256 & MD5 hw accelerator driver");
  906. MODULE_AUTHOR("Will Thomas.");
  907. MODULE_AUTHOR("James Hartley <james.hartley@imgtec.com>");