omap-des.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /*
  2. * Support for OMAP DES and Triple DES HW acceleration.
  3. *
  4. * Copyright (c) 2013 Texas Instruments Incorporated
  5. * Author: Joel Fernandes <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #ifdef DEBUG
  14. #define prn(num) printk(#num "=%d\n", num)
  15. #define prx(num) printk(#num "=%x\n", num)
  16. #else
  17. #define prn(num) do { } while (0)
  18. #define prx(num) do { } while (0)
  19. #endif
  20. #include <linux/err.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/errno.h>
  24. #include <linux/kernel.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/dmaengine.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/of.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_address.h>
  33. #include <linux/io.h>
  34. #include <linux/crypto.h>
  35. #include <linux/interrupt.h>
  36. #include <crypto/scatterwalk.h>
  37. #include <crypto/des.h>
  38. #include <crypto/algapi.h>
  39. #include <crypto/engine.h>
  40. #define DST_MAXBURST 2
  41. #define DES_BLOCK_WORDS (DES_BLOCK_SIZE >> 2)
  42. #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
  43. #define DES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
  44. ((x ^ 0x01) * 0x04))
  45. #define DES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
  46. #define DES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
  47. #define DES_REG_CTRL_CBC BIT(4)
  48. #define DES_REG_CTRL_TDES BIT(3)
  49. #define DES_REG_CTRL_DIRECTION BIT(2)
  50. #define DES_REG_CTRL_INPUT_READY BIT(1)
  51. #define DES_REG_CTRL_OUTPUT_READY BIT(0)
  52. #define DES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
  53. #define DES_REG_REV(dd) ((dd)->pdata->rev_ofs)
  54. #define DES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
  55. #define DES_REG_LENGTH_N(x) (0x24 + ((x) * 0x04))
  56. #define DES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
  57. #define DES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
  58. #define DES_REG_IRQ_DATA_IN BIT(1)
  59. #define DES_REG_IRQ_DATA_OUT BIT(2)
  60. #define FLAGS_MODE_MASK 0x000f
  61. #define FLAGS_ENCRYPT BIT(0)
  62. #define FLAGS_CBC BIT(1)
  63. #define FLAGS_INIT BIT(4)
  64. #define FLAGS_BUSY BIT(6)
  65. struct omap_des_ctx {
  66. struct omap_des_dev *dd;
  67. int keylen;
  68. u32 key[(3 * DES_KEY_SIZE) / sizeof(u32)];
  69. unsigned long flags;
  70. };
  71. struct omap_des_reqctx {
  72. unsigned long mode;
  73. };
  74. #define OMAP_DES_QUEUE_LENGTH 1
  75. #define OMAP_DES_CACHE_SIZE 0
  76. struct omap_des_algs_info {
  77. struct crypto_alg *algs_list;
  78. unsigned int size;
  79. unsigned int registered;
  80. };
  81. struct omap_des_pdata {
  82. struct omap_des_algs_info *algs_info;
  83. unsigned int algs_info_size;
  84. void (*trigger)(struct omap_des_dev *dd, int length);
  85. u32 key_ofs;
  86. u32 iv_ofs;
  87. u32 ctrl_ofs;
  88. u32 data_ofs;
  89. u32 rev_ofs;
  90. u32 mask_ofs;
  91. u32 irq_enable_ofs;
  92. u32 irq_status_ofs;
  93. u32 dma_enable_in;
  94. u32 dma_enable_out;
  95. u32 dma_start;
  96. u32 major_mask;
  97. u32 major_shift;
  98. u32 minor_mask;
  99. u32 minor_shift;
  100. };
  101. struct omap_des_dev {
  102. struct list_head list;
  103. unsigned long phys_base;
  104. void __iomem *io_base;
  105. struct omap_des_ctx *ctx;
  106. struct device *dev;
  107. unsigned long flags;
  108. int err;
  109. struct tasklet_struct done_task;
  110. struct ablkcipher_request *req;
  111. struct crypto_engine *engine;
  112. /*
  113. * total is used by PIO mode for book keeping so introduce
  114. * variable total_save as need it to calc page_order
  115. */
  116. size_t total;
  117. size_t total_save;
  118. struct scatterlist *in_sg;
  119. struct scatterlist *out_sg;
  120. /* Buffers for copying for unaligned cases */
  121. struct scatterlist in_sgl;
  122. struct scatterlist out_sgl;
  123. struct scatterlist *orig_out;
  124. int sgs_copied;
  125. struct scatter_walk in_walk;
  126. struct scatter_walk out_walk;
  127. struct dma_chan *dma_lch_in;
  128. struct dma_chan *dma_lch_out;
  129. int in_sg_len;
  130. int out_sg_len;
  131. int pio_only;
  132. const struct omap_des_pdata *pdata;
  133. };
  134. /* keep registered devices data here */
  135. static LIST_HEAD(dev_list);
  136. static DEFINE_SPINLOCK(list_lock);
  137. #ifdef DEBUG
  138. #define omap_des_read(dd, offset) \
  139. ({ \
  140. int _read_ret; \
  141. _read_ret = __raw_readl(dd->io_base + offset); \
  142. pr_err("omap_des_read(" #offset "=%#x)= %#x\n", \
  143. offset, _read_ret); \
  144. _read_ret; \
  145. })
  146. #else
  147. static inline u32 omap_des_read(struct omap_des_dev *dd, u32 offset)
  148. {
  149. return __raw_readl(dd->io_base + offset);
  150. }
  151. #endif
  152. #ifdef DEBUG
  153. #define omap_des_write(dd, offset, value) \
  154. do { \
  155. pr_err("omap_des_write(" #offset "=%#x) value=%#x\n", \
  156. offset, value); \
  157. __raw_writel(value, dd->io_base + offset); \
  158. } while (0)
  159. #else
  160. static inline void omap_des_write(struct omap_des_dev *dd, u32 offset,
  161. u32 value)
  162. {
  163. __raw_writel(value, dd->io_base + offset);
  164. }
  165. #endif
  166. static inline void omap_des_write_mask(struct omap_des_dev *dd, u32 offset,
  167. u32 value, u32 mask)
  168. {
  169. u32 val;
  170. val = omap_des_read(dd, offset);
  171. val &= ~mask;
  172. val |= value;
  173. omap_des_write(dd, offset, val);
  174. }
  175. static void omap_des_write_n(struct omap_des_dev *dd, u32 offset,
  176. u32 *value, int count)
  177. {
  178. for (; count--; value++, offset += 4)
  179. omap_des_write(dd, offset, *value);
  180. }
  181. static int omap_des_hw_init(struct omap_des_dev *dd)
  182. {
  183. int err;
  184. /*
  185. * clocks are enabled when request starts and disabled when finished.
  186. * It may be long delays between requests.
  187. * Device might go to off mode to save power.
  188. */
  189. err = pm_runtime_get_sync(dd->dev);
  190. if (err < 0) {
  191. pm_runtime_put_noidle(dd->dev);
  192. dev_err(dd->dev, "%s: failed to get_sync(%d)\n", __func__, err);
  193. return err;
  194. }
  195. if (!(dd->flags & FLAGS_INIT)) {
  196. dd->flags |= FLAGS_INIT;
  197. dd->err = 0;
  198. }
  199. return 0;
  200. }
  201. static int omap_des_write_ctrl(struct omap_des_dev *dd)
  202. {
  203. unsigned int key32;
  204. int i, err;
  205. u32 val = 0, mask = 0;
  206. err = omap_des_hw_init(dd);
  207. if (err)
  208. return err;
  209. key32 = dd->ctx->keylen / sizeof(u32);
  210. /* it seems a key should always be set even if it has not changed */
  211. for (i = 0; i < key32; i++) {
  212. omap_des_write(dd, DES_REG_KEY(dd, i),
  213. __le32_to_cpu(dd->ctx->key[i]));
  214. }
  215. if ((dd->flags & FLAGS_CBC) && dd->req->info)
  216. omap_des_write_n(dd, DES_REG_IV(dd, 0), dd->req->info, 2);
  217. if (dd->flags & FLAGS_CBC)
  218. val |= DES_REG_CTRL_CBC;
  219. if (dd->flags & FLAGS_ENCRYPT)
  220. val |= DES_REG_CTRL_DIRECTION;
  221. if (key32 == 6)
  222. val |= DES_REG_CTRL_TDES;
  223. mask |= DES_REG_CTRL_CBC | DES_REG_CTRL_DIRECTION | DES_REG_CTRL_TDES;
  224. omap_des_write_mask(dd, DES_REG_CTRL(dd), val, mask);
  225. return 0;
  226. }
  227. static void omap_des_dma_trigger_omap4(struct omap_des_dev *dd, int length)
  228. {
  229. u32 mask, val;
  230. omap_des_write(dd, DES_REG_LENGTH_N(0), length);
  231. val = dd->pdata->dma_start;
  232. if (dd->dma_lch_out != NULL)
  233. val |= dd->pdata->dma_enable_out;
  234. if (dd->dma_lch_in != NULL)
  235. val |= dd->pdata->dma_enable_in;
  236. mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
  237. dd->pdata->dma_start;
  238. omap_des_write_mask(dd, DES_REG_MASK(dd), val, mask);
  239. }
  240. static void omap_des_dma_stop(struct omap_des_dev *dd)
  241. {
  242. u32 mask;
  243. mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
  244. dd->pdata->dma_start;
  245. omap_des_write_mask(dd, DES_REG_MASK(dd), 0, mask);
  246. }
  247. static struct omap_des_dev *omap_des_find_dev(struct omap_des_ctx *ctx)
  248. {
  249. struct omap_des_dev *dd = NULL, *tmp;
  250. spin_lock_bh(&list_lock);
  251. if (!ctx->dd) {
  252. list_for_each_entry(tmp, &dev_list, list) {
  253. /* FIXME: take fist available des core */
  254. dd = tmp;
  255. break;
  256. }
  257. ctx->dd = dd;
  258. } else {
  259. /* already found before */
  260. dd = ctx->dd;
  261. }
  262. spin_unlock_bh(&list_lock);
  263. return dd;
  264. }
  265. static void omap_des_dma_out_callback(void *data)
  266. {
  267. struct omap_des_dev *dd = data;
  268. /* dma_lch_out - completed */
  269. tasklet_schedule(&dd->done_task);
  270. }
  271. static int omap_des_dma_init(struct omap_des_dev *dd)
  272. {
  273. int err;
  274. dd->dma_lch_out = NULL;
  275. dd->dma_lch_in = NULL;
  276. dd->dma_lch_in = dma_request_chan(dd->dev, "rx");
  277. if (IS_ERR(dd->dma_lch_in)) {
  278. dev_err(dd->dev, "Unable to request in DMA channel\n");
  279. return PTR_ERR(dd->dma_lch_in);
  280. }
  281. dd->dma_lch_out = dma_request_chan(dd->dev, "tx");
  282. if (IS_ERR(dd->dma_lch_out)) {
  283. dev_err(dd->dev, "Unable to request out DMA channel\n");
  284. err = PTR_ERR(dd->dma_lch_out);
  285. goto err_dma_out;
  286. }
  287. return 0;
  288. err_dma_out:
  289. dma_release_channel(dd->dma_lch_in);
  290. return err;
  291. }
  292. static void omap_des_dma_cleanup(struct omap_des_dev *dd)
  293. {
  294. if (dd->pio_only)
  295. return;
  296. dma_release_channel(dd->dma_lch_out);
  297. dma_release_channel(dd->dma_lch_in);
  298. }
  299. static void sg_copy_buf(void *buf, struct scatterlist *sg,
  300. unsigned int start, unsigned int nbytes, int out)
  301. {
  302. struct scatter_walk walk;
  303. if (!nbytes)
  304. return;
  305. scatterwalk_start(&walk, sg);
  306. scatterwalk_advance(&walk, start);
  307. scatterwalk_copychunks(buf, &walk, nbytes, out);
  308. scatterwalk_done(&walk, out, 0);
  309. }
  310. static int omap_des_crypt_dma(struct crypto_tfm *tfm,
  311. struct scatterlist *in_sg, struct scatterlist *out_sg,
  312. int in_sg_len, int out_sg_len)
  313. {
  314. struct omap_des_ctx *ctx = crypto_tfm_ctx(tfm);
  315. struct omap_des_dev *dd = ctx->dd;
  316. struct dma_async_tx_descriptor *tx_in, *tx_out;
  317. struct dma_slave_config cfg;
  318. int ret;
  319. if (dd->pio_only) {
  320. scatterwalk_start(&dd->in_walk, dd->in_sg);
  321. scatterwalk_start(&dd->out_walk, dd->out_sg);
  322. /* Enable DATAIN interrupt and let it take
  323. care of the rest */
  324. omap_des_write(dd, DES_REG_IRQ_ENABLE(dd), 0x2);
  325. return 0;
  326. }
  327. dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
  328. memset(&cfg, 0, sizeof(cfg));
  329. cfg.src_addr = dd->phys_base + DES_REG_DATA_N(dd, 0);
  330. cfg.dst_addr = dd->phys_base + DES_REG_DATA_N(dd, 0);
  331. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  332. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  333. cfg.src_maxburst = DST_MAXBURST;
  334. cfg.dst_maxburst = DST_MAXBURST;
  335. /* IN */
  336. ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
  337. if (ret) {
  338. dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
  339. ret);
  340. return ret;
  341. }
  342. tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
  343. DMA_MEM_TO_DEV,
  344. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  345. if (!tx_in) {
  346. dev_err(dd->dev, "IN prep_slave_sg() failed\n");
  347. return -EINVAL;
  348. }
  349. /* No callback necessary */
  350. tx_in->callback_param = dd;
  351. /* OUT */
  352. ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
  353. if (ret) {
  354. dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
  355. ret);
  356. return ret;
  357. }
  358. tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
  359. DMA_DEV_TO_MEM,
  360. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  361. if (!tx_out) {
  362. dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
  363. return -EINVAL;
  364. }
  365. tx_out->callback = omap_des_dma_out_callback;
  366. tx_out->callback_param = dd;
  367. dmaengine_submit(tx_in);
  368. dmaengine_submit(tx_out);
  369. dma_async_issue_pending(dd->dma_lch_in);
  370. dma_async_issue_pending(dd->dma_lch_out);
  371. /* start DMA */
  372. dd->pdata->trigger(dd, dd->total);
  373. return 0;
  374. }
  375. static int omap_des_crypt_dma_start(struct omap_des_dev *dd)
  376. {
  377. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
  378. crypto_ablkcipher_reqtfm(dd->req));
  379. int err;
  380. pr_debug("total: %d\n", dd->total);
  381. if (!dd->pio_only) {
  382. err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
  383. DMA_TO_DEVICE);
  384. if (!err) {
  385. dev_err(dd->dev, "dma_map_sg() error\n");
  386. return -EINVAL;
  387. }
  388. err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
  389. DMA_FROM_DEVICE);
  390. if (!err) {
  391. dev_err(dd->dev, "dma_map_sg() error\n");
  392. return -EINVAL;
  393. }
  394. }
  395. err = omap_des_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
  396. dd->out_sg_len);
  397. if (err && !dd->pio_only) {
  398. dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
  399. dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
  400. DMA_FROM_DEVICE);
  401. }
  402. return err;
  403. }
  404. static void omap_des_finish_req(struct omap_des_dev *dd, int err)
  405. {
  406. struct ablkcipher_request *req = dd->req;
  407. pr_debug("err: %d\n", err);
  408. pm_runtime_put(dd->dev);
  409. crypto_finalize_cipher_request(dd->engine, req, err);
  410. }
  411. static int omap_des_crypt_dma_stop(struct omap_des_dev *dd)
  412. {
  413. pr_debug("total: %d\n", dd->total);
  414. omap_des_dma_stop(dd);
  415. dmaengine_terminate_all(dd->dma_lch_in);
  416. dmaengine_terminate_all(dd->dma_lch_out);
  417. return 0;
  418. }
  419. static int omap_des_copy_needed(struct scatterlist *sg)
  420. {
  421. while (sg) {
  422. if (!IS_ALIGNED(sg->offset, 4))
  423. return -1;
  424. if (!IS_ALIGNED(sg->length, DES_BLOCK_SIZE))
  425. return -1;
  426. sg = sg_next(sg);
  427. }
  428. return 0;
  429. }
  430. static int omap_des_copy_sgs(struct omap_des_dev *dd)
  431. {
  432. void *buf_in, *buf_out;
  433. int pages;
  434. pages = dd->total >> PAGE_SHIFT;
  435. if (dd->total & (PAGE_SIZE-1))
  436. pages++;
  437. BUG_ON(!pages);
  438. buf_in = (void *)__get_free_pages(GFP_ATOMIC, pages);
  439. buf_out = (void *)__get_free_pages(GFP_ATOMIC, pages);
  440. if (!buf_in || !buf_out) {
  441. pr_err("Couldn't allocated pages for unaligned cases.\n");
  442. return -1;
  443. }
  444. dd->orig_out = dd->out_sg;
  445. sg_copy_buf(buf_in, dd->in_sg, 0, dd->total, 0);
  446. sg_init_table(&dd->in_sgl, 1);
  447. sg_set_buf(&dd->in_sgl, buf_in, dd->total);
  448. dd->in_sg = &dd->in_sgl;
  449. dd->in_sg_len = 1;
  450. sg_init_table(&dd->out_sgl, 1);
  451. sg_set_buf(&dd->out_sgl, buf_out, dd->total);
  452. dd->out_sg = &dd->out_sgl;
  453. dd->out_sg_len = 1;
  454. return 0;
  455. }
  456. static int omap_des_handle_queue(struct omap_des_dev *dd,
  457. struct ablkcipher_request *req)
  458. {
  459. if (req)
  460. return crypto_transfer_cipher_request_to_engine(dd->engine, req);
  461. return 0;
  462. }
  463. static int omap_des_prepare_req(struct crypto_engine *engine,
  464. struct ablkcipher_request *req)
  465. {
  466. struct omap_des_ctx *ctx = crypto_ablkcipher_ctx(
  467. crypto_ablkcipher_reqtfm(req));
  468. struct omap_des_dev *dd = omap_des_find_dev(ctx);
  469. struct omap_des_reqctx *rctx;
  470. if (!dd)
  471. return -ENODEV;
  472. /* assign new request to device */
  473. dd->req = req;
  474. dd->total = req->nbytes;
  475. dd->total_save = req->nbytes;
  476. dd->in_sg = req->src;
  477. dd->out_sg = req->dst;
  478. dd->in_sg_len = sg_nents_for_len(dd->in_sg, dd->total);
  479. if (dd->in_sg_len < 0)
  480. return dd->in_sg_len;
  481. dd->out_sg_len = sg_nents_for_len(dd->out_sg, dd->total);
  482. if (dd->out_sg_len < 0)
  483. return dd->out_sg_len;
  484. if (omap_des_copy_needed(dd->in_sg) ||
  485. omap_des_copy_needed(dd->out_sg)) {
  486. if (omap_des_copy_sgs(dd))
  487. pr_err("Failed to copy SGs for unaligned cases\n");
  488. dd->sgs_copied = 1;
  489. } else {
  490. dd->sgs_copied = 0;
  491. }
  492. rctx = ablkcipher_request_ctx(req);
  493. ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
  494. rctx->mode &= FLAGS_MODE_MASK;
  495. dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
  496. dd->ctx = ctx;
  497. ctx->dd = dd;
  498. return omap_des_write_ctrl(dd);
  499. }
  500. static int omap_des_crypt_req(struct crypto_engine *engine,
  501. struct ablkcipher_request *req)
  502. {
  503. struct omap_des_ctx *ctx = crypto_ablkcipher_ctx(
  504. crypto_ablkcipher_reqtfm(req));
  505. struct omap_des_dev *dd = omap_des_find_dev(ctx);
  506. if (!dd)
  507. return -ENODEV;
  508. return omap_des_crypt_dma_start(dd);
  509. }
  510. static void omap_des_done_task(unsigned long data)
  511. {
  512. struct omap_des_dev *dd = (struct omap_des_dev *)data;
  513. void *buf_in, *buf_out;
  514. int pages;
  515. pr_debug("enter done_task\n");
  516. if (!dd->pio_only) {
  517. dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
  518. DMA_FROM_DEVICE);
  519. dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
  520. dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
  521. DMA_FROM_DEVICE);
  522. omap_des_crypt_dma_stop(dd);
  523. }
  524. if (dd->sgs_copied) {
  525. buf_in = sg_virt(&dd->in_sgl);
  526. buf_out = sg_virt(&dd->out_sgl);
  527. sg_copy_buf(buf_out, dd->orig_out, 0, dd->total_save, 1);
  528. pages = get_order(dd->total_save);
  529. free_pages((unsigned long)buf_in, pages);
  530. free_pages((unsigned long)buf_out, pages);
  531. }
  532. omap_des_finish_req(dd, 0);
  533. pr_debug("exit\n");
  534. }
  535. static int omap_des_crypt(struct ablkcipher_request *req, unsigned long mode)
  536. {
  537. struct omap_des_ctx *ctx = crypto_ablkcipher_ctx(
  538. crypto_ablkcipher_reqtfm(req));
  539. struct omap_des_reqctx *rctx = ablkcipher_request_ctx(req);
  540. struct omap_des_dev *dd;
  541. pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
  542. !!(mode & FLAGS_ENCRYPT),
  543. !!(mode & FLAGS_CBC));
  544. if (!IS_ALIGNED(req->nbytes, DES_BLOCK_SIZE)) {
  545. pr_err("request size is not exact amount of DES blocks\n");
  546. return -EINVAL;
  547. }
  548. dd = omap_des_find_dev(ctx);
  549. if (!dd)
  550. return -ENODEV;
  551. rctx->mode = mode;
  552. return omap_des_handle_queue(dd, req);
  553. }
  554. /* ********************** ALG API ************************************ */
  555. static int omap_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  556. unsigned int keylen)
  557. {
  558. struct omap_des_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  559. if (keylen != DES_KEY_SIZE && keylen != (3*DES_KEY_SIZE))
  560. return -EINVAL;
  561. pr_debug("enter, keylen: %d\n", keylen);
  562. memcpy(ctx->key, key, keylen);
  563. ctx->keylen = keylen;
  564. return 0;
  565. }
  566. static int omap_des_ecb_encrypt(struct ablkcipher_request *req)
  567. {
  568. return omap_des_crypt(req, FLAGS_ENCRYPT);
  569. }
  570. static int omap_des_ecb_decrypt(struct ablkcipher_request *req)
  571. {
  572. return omap_des_crypt(req, 0);
  573. }
  574. static int omap_des_cbc_encrypt(struct ablkcipher_request *req)
  575. {
  576. return omap_des_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
  577. }
  578. static int omap_des_cbc_decrypt(struct ablkcipher_request *req)
  579. {
  580. return omap_des_crypt(req, FLAGS_CBC);
  581. }
  582. static int omap_des_cra_init(struct crypto_tfm *tfm)
  583. {
  584. pr_debug("enter\n");
  585. tfm->crt_ablkcipher.reqsize = sizeof(struct omap_des_reqctx);
  586. return 0;
  587. }
  588. static void omap_des_cra_exit(struct crypto_tfm *tfm)
  589. {
  590. pr_debug("enter\n");
  591. }
  592. /* ********************** ALGS ************************************ */
  593. static struct crypto_alg algs_ecb_cbc[] = {
  594. {
  595. .cra_name = "ecb(des)",
  596. .cra_driver_name = "ecb-des-omap",
  597. .cra_priority = 100,
  598. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  599. CRYPTO_ALG_KERN_DRIVER_ONLY |
  600. CRYPTO_ALG_ASYNC,
  601. .cra_blocksize = DES_BLOCK_SIZE,
  602. .cra_ctxsize = sizeof(struct omap_des_ctx),
  603. .cra_alignmask = 0,
  604. .cra_type = &crypto_ablkcipher_type,
  605. .cra_module = THIS_MODULE,
  606. .cra_init = omap_des_cra_init,
  607. .cra_exit = omap_des_cra_exit,
  608. .cra_u.ablkcipher = {
  609. .min_keysize = DES_KEY_SIZE,
  610. .max_keysize = DES_KEY_SIZE,
  611. .setkey = omap_des_setkey,
  612. .encrypt = omap_des_ecb_encrypt,
  613. .decrypt = omap_des_ecb_decrypt,
  614. }
  615. },
  616. {
  617. .cra_name = "cbc(des)",
  618. .cra_driver_name = "cbc-des-omap",
  619. .cra_priority = 100,
  620. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  621. CRYPTO_ALG_KERN_DRIVER_ONLY |
  622. CRYPTO_ALG_ASYNC,
  623. .cra_blocksize = DES_BLOCK_SIZE,
  624. .cra_ctxsize = sizeof(struct omap_des_ctx),
  625. .cra_alignmask = 0,
  626. .cra_type = &crypto_ablkcipher_type,
  627. .cra_module = THIS_MODULE,
  628. .cra_init = omap_des_cra_init,
  629. .cra_exit = omap_des_cra_exit,
  630. .cra_u.ablkcipher = {
  631. .min_keysize = DES_KEY_SIZE,
  632. .max_keysize = DES_KEY_SIZE,
  633. .ivsize = DES_BLOCK_SIZE,
  634. .setkey = omap_des_setkey,
  635. .encrypt = omap_des_cbc_encrypt,
  636. .decrypt = omap_des_cbc_decrypt,
  637. }
  638. },
  639. {
  640. .cra_name = "ecb(des3_ede)",
  641. .cra_driver_name = "ecb-des3-omap",
  642. .cra_priority = 100,
  643. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  644. CRYPTO_ALG_KERN_DRIVER_ONLY |
  645. CRYPTO_ALG_ASYNC,
  646. .cra_blocksize = DES_BLOCK_SIZE,
  647. .cra_ctxsize = sizeof(struct omap_des_ctx),
  648. .cra_alignmask = 0,
  649. .cra_type = &crypto_ablkcipher_type,
  650. .cra_module = THIS_MODULE,
  651. .cra_init = omap_des_cra_init,
  652. .cra_exit = omap_des_cra_exit,
  653. .cra_u.ablkcipher = {
  654. .min_keysize = 3*DES_KEY_SIZE,
  655. .max_keysize = 3*DES_KEY_SIZE,
  656. .setkey = omap_des_setkey,
  657. .encrypt = omap_des_ecb_encrypt,
  658. .decrypt = omap_des_ecb_decrypt,
  659. }
  660. },
  661. {
  662. .cra_name = "cbc(des3_ede)",
  663. .cra_driver_name = "cbc-des3-omap",
  664. .cra_priority = 100,
  665. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  666. CRYPTO_ALG_KERN_DRIVER_ONLY |
  667. CRYPTO_ALG_ASYNC,
  668. .cra_blocksize = DES_BLOCK_SIZE,
  669. .cra_ctxsize = sizeof(struct omap_des_ctx),
  670. .cra_alignmask = 0,
  671. .cra_type = &crypto_ablkcipher_type,
  672. .cra_module = THIS_MODULE,
  673. .cra_init = omap_des_cra_init,
  674. .cra_exit = omap_des_cra_exit,
  675. .cra_u.ablkcipher = {
  676. .min_keysize = 3*DES_KEY_SIZE,
  677. .max_keysize = 3*DES_KEY_SIZE,
  678. .ivsize = DES_BLOCK_SIZE,
  679. .setkey = omap_des_setkey,
  680. .encrypt = omap_des_cbc_encrypt,
  681. .decrypt = omap_des_cbc_decrypt,
  682. }
  683. }
  684. };
  685. static struct omap_des_algs_info omap_des_algs_info_ecb_cbc[] = {
  686. {
  687. .algs_list = algs_ecb_cbc,
  688. .size = ARRAY_SIZE(algs_ecb_cbc),
  689. },
  690. };
  691. #ifdef CONFIG_OF
  692. static const struct omap_des_pdata omap_des_pdata_omap4 = {
  693. .algs_info = omap_des_algs_info_ecb_cbc,
  694. .algs_info_size = ARRAY_SIZE(omap_des_algs_info_ecb_cbc),
  695. .trigger = omap_des_dma_trigger_omap4,
  696. .key_ofs = 0x14,
  697. .iv_ofs = 0x18,
  698. .ctrl_ofs = 0x20,
  699. .data_ofs = 0x28,
  700. .rev_ofs = 0x30,
  701. .mask_ofs = 0x34,
  702. .irq_status_ofs = 0x3c,
  703. .irq_enable_ofs = 0x40,
  704. .dma_enable_in = BIT(5),
  705. .dma_enable_out = BIT(6),
  706. .major_mask = 0x0700,
  707. .major_shift = 8,
  708. .minor_mask = 0x003f,
  709. .minor_shift = 0,
  710. };
  711. static irqreturn_t omap_des_irq(int irq, void *dev_id)
  712. {
  713. struct omap_des_dev *dd = dev_id;
  714. u32 status, i;
  715. u32 *src, *dst;
  716. status = omap_des_read(dd, DES_REG_IRQ_STATUS(dd));
  717. if (status & DES_REG_IRQ_DATA_IN) {
  718. omap_des_write(dd, DES_REG_IRQ_ENABLE(dd), 0x0);
  719. BUG_ON(!dd->in_sg);
  720. BUG_ON(_calc_walked(in) > dd->in_sg->length);
  721. src = sg_virt(dd->in_sg) + _calc_walked(in);
  722. for (i = 0; i < DES_BLOCK_WORDS; i++) {
  723. omap_des_write(dd, DES_REG_DATA_N(dd, i), *src);
  724. scatterwalk_advance(&dd->in_walk, 4);
  725. if (dd->in_sg->length == _calc_walked(in)) {
  726. dd->in_sg = sg_next(dd->in_sg);
  727. if (dd->in_sg) {
  728. scatterwalk_start(&dd->in_walk,
  729. dd->in_sg);
  730. src = sg_virt(dd->in_sg) +
  731. _calc_walked(in);
  732. }
  733. } else {
  734. src++;
  735. }
  736. }
  737. /* Clear IRQ status */
  738. status &= ~DES_REG_IRQ_DATA_IN;
  739. omap_des_write(dd, DES_REG_IRQ_STATUS(dd), status);
  740. /* Enable DATA_OUT interrupt */
  741. omap_des_write(dd, DES_REG_IRQ_ENABLE(dd), 0x4);
  742. } else if (status & DES_REG_IRQ_DATA_OUT) {
  743. omap_des_write(dd, DES_REG_IRQ_ENABLE(dd), 0x0);
  744. BUG_ON(!dd->out_sg);
  745. BUG_ON(_calc_walked(out) > dd->out_sg->length);
  746. dst = sg_virt(dd->out_sg) + _calc_walked(out);
  747. for (i = 0; i < DES_BLOCK_WORDS; i++) {
  748. *dst = omap_des_read(dd, DES_REG_DATA_N(dd, i));
  749. scatterwalk_advance(&dd->out_walk, 4);
  750. if (dd->out_sg->length == _calc_walked(out)) {
  751. dd->out_sg = sg_next(dd->out_sg);
  752. if (dd->out_sg) {
  753. scatterwalk_start(&dd->out_walk,
  754. dd->out_sg);
  755. dst = sg_virt(dd->out_sg) +
  756. _calc_walked(out);
  757. }
  758. } else {
  759. dst++;
  760. }
  761. }
  762. BUG_ON(dd->total < DES_BLOCK_SIZE);
  763. dd->total -= DES_BLOCK_SIZE;
  764. /* Clear IRQ status */
  765. status &= ~DES_REG_IRQ_DATA_OUT;
  766. omap_des_write(dd, DES_REG_IRQ_STATUS(dd), status);
  767. if (!dd->total)
  768. /* All bytes read! */
  769. tasklet_schedule(&dd->done_task);
  770. else
  771. /* Enable DATA_IN interrupt for next block */
  772. omap_des_write(dd, DES_REG_IRQ_ENABLE(dd), 0x2);
  773. }
  774. return IRQ_HANDLED;
  775. }
  776. static const struct of_device_id omap_des_of_match[] = {
  777. {
  778. .compatible = "ti,omap4-des",
  779. .data = &omap_des_pdata_omap4,
  780. },
  781. {},
  782. };
  783. MODULE_DEVICE_TABLE(of, omap_des_of_match);
  784. static int omap_des_get_of(struct omap_des_dev *dd,
  785. struct platform_device *pdev)
  786. {
  787. const struct of_device_id *match;
  788. match = of_match_device(of_match_ptr(omap_des_of_match), &pdev->dev);
  789. if (!match) {
  790. dev_err(&pdev->dev, "no compatible OF match\n");
  791. return -EINVAL;
  792. }
  793. dd->pdata = match->data;
  794. return 0;
  795. }
  796. #else
  797. static int omap_des_get_of(struct omap_des_dev *dd,
  798. struct device *dev)
  799. {
  800. return -EINVAL;
  801. }
  802. #endif
  803. static int omap_des_get_pdev(struct omap_des_dev *dd,
  804. struct platform_device *pdev)
  805. {
  806. /* non-DT devices get pdata from pdev */
  807. dd->pdata = pdev->dev.platform_data;
  808. return 0;
  809. }
  810. static int omap_des_probe(struct platform_device *pdev)
  811. {
  812. struct device *dev = &pdev->dev;
  813. struct omap_des_dev *dd;
  814. struct crypto_alg *algp;
  815. struct resource *res;
  816. int err = -ENOMEM, i, j, irq = -1;
  817. u32 reg;
  818. dd = devm_kzalloc(dev, sizeof(struct omap_des_dev), GFP_KERNEL);
  819. if (dd == NULL) {
  820. dev_err(dev, "unable to alloc data struct.\n");
  821. goto err_data;
  822. }
  823. dd->dev = dev;
  824. platform_set_drvdata(pdev, dd);
  825. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  826. if (!res) {
  827. dev_err(dev, "no MEM resource info\n");
  828. goto err_res;
  829. }
  830. err = (dev->of_node) ? omap_des_get_of(dd, pdev) :
  831. omap_des_get_pdev(dd, pdev);
  832. if (err)
  833. goto err_res;
  834. dd->io_base = devm_ioremap_resource(dev, res);
  835. if (IS_ERR(dd->io_base)) {
  836. err = PTR_ERR(dd->io_base);
  837. goto err_res;
  838. }
  839. dd->phys_base = res->start;
  840. pm_runtime_enable(dev);
  841. pm_runtime_irq_safe(dev);
  842. err = pm_runtime_get_sync(dev);
  843. if (err < 0) {
  844. pm_runtime_put_noidle(dev);
  845. dev_err(dd->dev, "%s: failed to get_sync(%d)\n", __func__, err);
  846. goto err_get;
  847. }
  848. omap_des_dma_stop(dd);
  849. reg = omap_des_read(dd, DES_REG_REV(dd));
  850. pm_runtime_put_sync(dev);
  851. dev_info(dev, "OMAP DES hw accel rev: %u.%u\n",
  852. (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
  853. (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
  854. tasklet_init(&dd->done_task, omap_des_done_task, (unsigned long)dd);
  855. err = omap_des_dma_init(dd);
  856. if (err == -EPROBE_DEFER) {
  857. goto err_irq;
  858. } else if (err && DES_REG_IRQ_STATUS(dd) && DES_REG_IRQ_ENABLE(dd)) {
  859. dd->pio_only = 1;
  860. irq = platform_get_irq(pdev, 0);
  861. if (irq < 0) {
  862. dev_err(dev, "can't get IRQ resource\n");
  863. goto err_irq;
  864. }
  865. err = devm_request_irq(dev, irq, omap_des_irq, 0,
  866. dev_name(dev), dd);
  867. if (err) {
  868. dev_err(dev, "Unable to grab omap-des IRQ\n");
  869. goto err_irq;
  870. }
  871. }
  872. INIT_LIST_HEAD(&dd->list);
  873. spin_lock(&list_lock);
  874. list_add_tail(&dd->list, &dev_list);
  875. spin_unlock(&list_lock);
  876. /* Initialize des crypto engine */
  877. dd->engine = crypto_engine_alloc_init(dev, 1);
  878. if (!dd->engine) {
  879. err = -ENOMEM;
  880. goto err_engine;
  881. }
  882. dd->engine->prepare_cipher_request = omap_des_prepare_req;
  883. dd->engine->cipher_one_request = omap_des_crypt_req;
  884. err = crypto_engine_start(dd->engine);
  885. if (err)
  886. goto err_engine;
  887. for (i = 0; i < dd->pdata->algs_info_size; i++) {
  888. for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
  889. algp = &dd->pdata->algs_info[i].algs_list[j];
  890. pr_debug("reg alg: %s\n", algp->cra_name);
  891. INIT_LIST_HEAD(&algp->cra_list);
  892. err = crypto_register_alg(algp);
  893. if (err)
  894. goto err_algs;
  895. dd->pdata->algs_info[i].registered++;
  896. }
  897. }
  898. return 0;
  899. err_algs:
  900. for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
  901. for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
  902. crypto_unregister_alg(
  903. &dd->pdata->algs_info[i].algs_list[j]);
  904. err_engine:
  905. if (dd->engine)
  906. crypto_engine_exit(dd->engine);
  907. omap_des_dma_cleanup(dd);
  908. err_irq:
  909. tasklet_kill(&dd->done_task);
  910. err_get:
  911. pm_runtime_disable(dev);
  912. err_res:
  913. dd = NULL;
  914. err_data:
  915. dev_err(dev, "initialization failed.\n");
  916. return err;
  917. }
  918. static int omap_des_remove(struct platform_device *pdev)
  919. {
  920. struct omap_des_dev *dd = platform_get_drvdata(pdev);
  921. int i, j;
  922. if (!dd)
  923. return -ENODEV;
  924. spin_lock(&list_lock);
  925. list_del(&dd->list);
  926. spin_unlock(&list_lock);
  927. for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
  928. for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
  929. crypto_unregister_alg(
  930. &dd->pdata->algs_info[i].algs_list[j]);
  931. tasklet_kill(&dd->done_task);
  932. omap_des_dma_cleanup(dd);
  933. pm_runtime_disable(dd->dev);
  934. dd = NULL;
  935. return 0;
  936. }
  937. #ifdef CONFIG_PM_SLEEP
  938. static int omap_des_suspend(struct device *dev)
  939. {
  940. pm_runtime_put_sync(dev);
  941. return 0;
  942. }
  943. static int omap_des_resume(struct device *dev)
  944. {
  945. int err;
  946. err = pm_runtime_get_sync(dev);
  947. if (err < 0) {
  948. pm_runtime_put_noidle(dev);
  949. dev_err(dev, "%s: failed to get_sync(%d)\n", __func__, err);
  950. return err;
  951. }
  952. return 0;
  953. }
  954. #endif
  955. static SIMPLE_DEV_PM_OPS(omap_des_pm_ops, omap_des_suspend, omap_des_resume);
  956. static struct platform_driver omap_des_driver = {
  957. .probe = omap_des_probe,
  958. .remove = omap_des_remove,
  959. .driver = {
  960. .name = "omap-des",
  961. .pm = &omap_des_pm_ops,
  962. .of_match_table = of_match_ptr(omap_des_of_match),
  963. },
  964. };
  965. module_platform_driver(omap_des_driver);
  966. MODULE_DESCRIPTION("OMAP DES hw acceleration support.");
  967. MODULE_LICENSE("GPL v2");
  968. MODULE_AUTHOR("Joel Fernandes <[email protected]>");