omap-des.c 29 KB

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