ifxmips_async_des.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /******************************************************************************
  2. **
  3. ** FILE NAME : ifxmips_async_des.c
  4. ** PROJECT : IFX UEIP
  5. ** MODULES : DEU Module
  6. **
  7. ** DATE : October 11, 2010
  8. ** AUTHOR : Mohammad Firdaus
  9. ** DESCRIPTION : Data Encryption Unit Driver for DES Algorithm
  10. ** COPYRIGHT : Copyright (c) 2010
  11. ** Infineon Technologies AG
  12. ** Am Campeon 1-12, 85579 Neubiberg, Germany
  13. **
  14. ** This program is free software; you can redistribute it and/or modify
  15. ** it under the terms of the GNU General Public License as published by
  16. ** the Free Software Foundation; either version 2 of the License, or
  17. ** (at your option) any later version.
  18. **
  19. ** HISTORY
  20. ** $Date $Author $Comment
  21. ** 08,Sept 2009 Mohammad Firdaus Initial UEIP release
  22. ** 11, Oct 2010 Mohammad Firdaus Kernel Port incl. Async. Ablkcipher mode
  23. ** 21,March 2011 Mohammad Firdaus Changes for Kernel 2.6.32 and IPSec integration
  24. *******************************************************************************/
  25. /*!
  26. \defgroup IFX_DEU IFX_DEU_DRIVERS
  27. \ingroup API
  28. \brief ifx DEU driver module
  29. */
  30. /*!
  31. \file ifxmips_async_des.c
  32. \ingroup IFX_DEU
  33. \brief DES Encryption Driver main file
  34. */
  35. /*!
  36. \defgroup IFX_DES_FUNCTIONS IFX_DES_FUNCTIONS
  37. \ingroup IFX_DEU
  38. \brief IFX DES driver Functions
  39. */
  40. #include <linux/wait.h>
  41. #include <linux/crypto.h>
  42. #include <linux/kernel.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/list.h>
  46. #include <crypto/ctr.h>
  47. #include <crypto/aes.h>
  48. #include <crypto/algapi.h>
  49. #include <crypto/scatterwalk.h>
  50. #include <asm/ifx/ifx_regs.h>
  51. #include <asm/ifx/ifx_types.h>
  52. #include <asm/ifx/common_routines.h>
  53. #include <asm/ifx/irq.h>
  54. #include <asm/ifx/ifx_pmu.h>
  55. #include <asm/ifx/ifx_gpio.h>
  56. #include <asm/kmap_types.h>
  57. #include "ifxmips_deu.h"
  58. #if defined(CONFIG_DANUBE)
  59. #include "ifxmips_deu_danube.h"
  60. extern int ifx_danube_pre_1_4;
  61. #elif defined(CONFIG_AR9)
  62. #include "ifxmips_deu_ar9.h"
  63. #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
  64. #include "ifxmips_deu_vr9.h"
  65. #else
  66. #error "Unkown platform"
  67. #endif
  68. /* DMA specific header and variables */
  69. spinlock_t des_lock;
  70. #define CRTCL_SECT_INIT spin_lock_init(&des_lock)
  71. #define CRTCL_SECT_START spin_lock_irqsave(&des_lock, flag)
  72. #define CRTCL_SECT_END spin_unlock_irqrestore(&des_lock, flag)
  73. /* Preprocessor declerations */
  74. #ifdef CRYPTO_DEBUG
  75. extern char debug_level;
  76. #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
  77. #else
  78. #define DPRINTF(level, format, args...)
  79. #endif
  80. //#define DES_3DES_START IFX_DES_CON
  81. #define DES_KEY_SIZE 8
  82. #define DES_EXPKEY_WORDS 32
  83. #define DES_BLOCK_SIZE 8
  84. #define DES3_EDE_KEY_SIZE (3 * DES_KEY_SIZE)
  85. #define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS)
  86. #define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE
  87. /* Function Declaration to prevent warning messages */
  88. void des_chip_init (void);
  89. u32 endian_swap(u32 input);
  90. u32 input_swap(u32 input);
  91. int aes_memory_allocate(int value);
  92. int des_memory_allocate(int value);
  93. void memory_release(u32 *buffer);
  94. u32* memory_alignment(const u8 *arg, u32 *buff_alloc, int in_out, int nbytes);
  95. void aes_dma_memory_copy(u32 *outcopy, u32 *out_dma, u8 *out_arg, int nbytes);
  96. void des_dma_memory_copy(u32 *outcopy, u32 *out_dma, u8 *out_arg, int nbytes);
  97. static int lq_deu_des_core (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
  98. u8 *iv_arg, u32 nbytes, int encdec, int mode);
  99. struct des_ctx {
  100. int controlr_M;
  101. int key_length;
  102. u8 iv[DES_BLOCK_SIZE];
  103. u32 expkey[DES3_EDE_EXPKEY_WORDS];
  104. };
  105. static int disable_multiblock = 0;
  106. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  107. module_param(disable_multiblock, int, 0);
  108. #else
  109. MODULE_PARM_DESC(disable_multiblock, "Disable encryption of whole multiblock buffers");
  110. #endif
  111. static int disable_deudma = 1;
  112. struct des_container {
  113. u8 *iv;
  114. u8 *dst_buf;
  115. u8 *src_buf;
  116. int mode;
  117. int encdec;
  118. int complete;
  119. int flag;
  120. u32 bytes_processed;
  121. u32 nbytes;
  122. struct ablkcipher_request arequest;
  123. };
  124. des_priv_t *des_queue;
  125. extern deu_drv_priv_t deu_dma_priv;
  126. void hexdump1(unsigned char *buf, unsigned int len)
  127. {
  128. print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
  129. 16, 1,
  130. buf, len, false);
  131. }
  132. /*! \fn int lq_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key, unsigned int keylen)
  133. * \ingroup IFX_DES_FUNCTIONS
  134. * \brief sets DES key
  135. * \param tfm linux crypto algo transform
  136. * \param key input key
  137. * \param keylen key length
  138. */
  139. static int lq_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  140. unsigned int keylen)
  141. {
  142. struct des_ctx *dctx = crypto_ablkcipher_ctx(tfm);
  143. //printk("setkey in %s\n", __FILE__);
  144. dctx->controlr_M = 0; // des
  145. dctx->key_length = keylen;
  146. memcpy ((u8 *) (dctx->expkey), key, keylen);
  147. return 0;
  148. }
  149. /*! \fn int lq_des3_ede_setkey(struct crypto_ablkcipher *tfm, const u8 *key, unsigned int keylen)
  150. * \ingroup IFX_DES_FUNCTIONS
  151. * \brief sets DES key
  152. * \param tfm linux crypto algo transform
  153. * \param key input key
  154. * \param keylen key length
  155. */
  156. static int lq_des3_ede_setkey(struct crypto_ablkcipher *tfm, const u8 *in_key,
  157. unsigned int keylen)
  158. {
  159. struct des_ctx *dctx = crypto_ablkcipher_ctx(tfm);
  160. //printk("setkey in %s\n", __FILE__);
  161. dctx->controlr_M = keylen/8 + 1; // des
  162. dctx->key_length = keylen;
  163. memcpy ((u8 *) (dctx->expkey), in_key, keylen);
  164. return 0;
  165. }
  166. /*! \fn void ifx_deu_des_core(void *ctx_arg, u8 *out_arg, const u8 *in_arg, u8 *iv_arg, u32 nbytes, int encdec, int mode)
  167. * \ingroup IFX_DES_FUNCTIONS
  168. * \brief main interface to DES hardware
  169. * \param ctx_arg crypto algo context
  170. * \param out_arg output bytestream
  171. * \param in_arg input bytestream
  172. * \param iv_arg initialization vector
  173. * \param nbytes length of bytestream
  174. * \param encdec 1 for encrypt; 0 for decrypt
  175. * \param mode operation mode such as ebc, cbc
  176. */
  177. static int lq_deu_des_core (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
  178. u8 *iv_arg, u32 nbytes, int encdec, int mode)
  179. {
  180. volatile struct des_t *des = (struct des_t *) DES_3DES_START;
  181. struct des_ctx *dctx = ctx_arg;
  182. u32 *key = dctx->expkey;
  183. unsigned long flag;
  184. int i = 0;
  185. int nblocks = 0;
  186. CRTCL_SECT_START;
  187. des->controlr.M = dctx->controlr_M;
  188. if (dctx->controlr_M == 0) // des
  189. {
  190. des->K1HR = DEU_ENDIAN_SWAP(*((u32 *) key + 0));
  191. des->K1LR = DEU_ENDIAN_SWAP(*((u32 *) key + 1));
  192. }
  193. else {
  194. /* Hardware Section */
  195. switch (dctx->key_length) {
  196. case 24:
  197. des->K3HR = DEU_ENDIAN_SWAP(*((u32 *) key + 4));
  198. des->K3LR = DEU_ENDIAN_SWAP(*((u32 *) key + 5));
  199. /* no break; */
  200. case 16:
  201. des->K2HR = DEU_ENDIAN_SWAP(*((u32 *) key + 2));
  202. des->K2LR = DEU_ENDIAN_SWAP(*((u32 *) key + 3));
  203. /* no break; */
  204. case 8:
  205. des->K1HR = DEU_ENDIAN_SWAP(*((u32 *) key + 0));
  206. des->K1LR = DEU_ENDIAN_SWAP(*((u32 *) key + 1));
  207. break;
  208. default:
  209. CRTCL_SECT_END;
  210. return -EINVAL;
  211. }
  212. }
  213. des->controlr.E_D = !encdec; //encryption
  214. des->controlr.O = mode; //0 ECB 1 CBC 2 OFB 3 CFB 4 CTR hexdump(prin,sizeof(*des));
  215. if (mode > 0) {
  216. des->IVHR = DEU_ENDIAN_SWAP(*(u32 *) iv_arg);
  217. des->IVLR = DEU_ENDIAN_SWAP(*((u32 *) iv_arg + 1));
  218. };
  219. /* memory alignment issue */
  220. dword_mem_aligned_in = (u32 *) DEU_DWORD_REORDERING(in_arg, des_buff_in, BUFFER_IN, nbytes);
  221. deu_priv->deu_rx_buf = (u32 *) out_arg;
  222. deu_priv->deu_rx_len = nbytes;
  223. dma->controlr.ALGO = 0; //DES
  224. des->controlr.DAU = 0;
  225. dma->controlr.BS = 0;
  226. dma->controlr.EN = 1;
  227. while (des->controlr.BUS) {
  228. };
  229. wlen = dma_device_write (dma_device, (u8 *) dword_mem_aligned_in, nbytes, NULL);
  230. if (wlen != nbytes) {
  231. dma->controlr.EN = 0;
  232. CRTCL_SECT_END;
  233. printk (KERN_ERR "[%s %s %d]: dma_device_write fail!\n", __FILE__, __func__, __LINE__);
  234. return -EINVAL;
  235. }
  236. /* Prepare Rx buf length used in dma psuedo interrupt */
  237. outcopy = (u32 *) DEU_DWORD_REORDERING(out_arg, des_buff_out, BUFFER_OUT, nbytes);
  238. deu_priv->outcopy = outcopy;
  239. deu_priv->event_src = DES_ASYNC_EVENT;
  240. if (mode > 0) {
  241. *(u32 *) iv_arg = DEU_ENDIAN_SWAP(des->IVHR);
  242. *((u32 *) iv_arg + 1) = DEU_ENDIAN_SWAP(des->IVLR);
  243. };
  244. CRTCL_SECT_END;
  245. return -EINPROGRESS;
  246. }
  247. static int count_sgs(struct scatterlist *sl, unsigned int total_bytes)
  248. {
  249. int i = 0;
  250. do {
  251. total_bytes -= sl[i].length;
  252. i++;
  253. } while (total_bytes > 0);
  254. return i;
  255. }
  256. /* \fn static inline struct des_container *des_container_cast (
  257. * struct scatterlist *dst)
  258. * \ingroup IFX_DES_FUNCTIONS
  259. * \brief Locate the structure des_container in memory.
  260. * \param *areq Pointer to memory location where ablkcipher_request is located
  261. * \return *des_cointainer The function pointer to des_container
  262. */
  263. static inline struct des_container *des_container_cast(
  264. struct ablkcipher_request *areq)
  265. {
  266. return container_of(areq, struct des_container, arequest);
  267. }
  268. /* \fn static void lq_sg_complete(struct des_container *des_con)
  269. * \ingroup IFX_DES_FUNCTIONS
  270. * \brief Free the used up memory after encryt/decrypt.
  271. */
  272. static void lq_sg_complete(struct des_container *des_con)
  273. {
  274. unsigned long queue_flag;
  275. spin_lock_irqsave(&des_queue->lock, queue_flag);
  276. kfree(des_con);
  277. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  278. }
  279. /* \fn void lq_sg_init(struct scatterlist *src,
  280. * struct scatterlist *dst)
  281. * \ingroup IFX_DES_FUNCTIONS
  282. * \brief Maps the scatterlists into a source/destination page.
  283. * \param *src Pointer to the source scatterlist
  284. * \param *dst Pointer to the destination scatterlist
  285. */
  286. static void lq_sg_init(struct des_container *des_con, struct scatterlist *src,
  287. struct scatterlist *dst)
  288. {
  289. struct page *dst_page, *src_page;
  290. src_page = sg_virt(src);
  291. des_con->src_buf = (char *) src_page;
  292. dst_page = sg_virt(dst);
  293. des_con->dst_buf = (char *) dst_page;
  294. }
  295. /* \fn static int process_next_packet(struct des_container *des_con, struct ablkcipher_request *areq,
  296. * int state)
  297. * \ingroup IFX_DES_FUNCTIONS
  298. * \brief Process the next packet after dequeuing the packet from crypto queue
  299. * \param *des_con Pointer to DES container structure
  300. * \param *areq Pointer to ablkcipher_request container
  301. * \param state State of the packet (scattered packet or new packet to be processed)
  302. * \return -EINVAL: DEU failure, -EINPROGRESS: DEU encrypt/decrypt in progress, 1: no scatterlist left
  303. */
  304. static int process_next_packet(struct des_container *des_con, struct ablkcipher_request *areq,
  305. int state)
  306. {
  307. u8 *iv;
  308. int mode, encdec, err = -EINVAL;
  309. u32 remain, inc, chunk_size, nbytes;
  310. struct scatterlist *src = NULL;
  311. struct scatterlist *dst = NULL;
  312. struct crypto_ablkcipher *cipher;
  313. struct des_ctx *ctx;
  314. unsigned long queue_flag;
  315. spin_lock_irqsave(&des_queue->lock, queue_flag);
  316. mode = des_con->mode;
  317. encdec = des_con->encdec;
  318. iv = des_con->iv;
  319. if (state & PROCESS_SCATTER) {
  320. src = scatterwalk_sg_next(areq->src);
  321. dst = scatterwalk_sg_next(areq->dst);
  322. if (!src || !dst) {
  323. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  324. return 1;
  325. }
  326. }
  327. else if (state & PROCESS_NEW_PACKET) {
  328. src = areq->src;
  329. dst = areq->dst;
  330. }
  331. remain = des_con->bytes_processed;
  332. chunk_size = src->length;
  333. //printk("debug ln: %d, func: %s, reqsize: %d, scattersize: %d\n",
  334. // __LINE__, __func__, areq->nbytes, chunk_size);
  335. if (remain > DEU_MAX_PACKET_SIZE)
  336. inc = DEU_MAX_PACKET_SIZE;
  337. else if(remain > chunk_size)
  338. inc = chunk_size;
  339. else
  340. inc = remain;
  341. remain -= inc;
  342. des_con->nbytes = inc;
  343. if (state & PROCESS_SCATTER) {
  344. des_con->src_buf += des_con->nbytes;
  345. des_con->dst_buf += des_con->nbytes;
  346. }
  347. lq_sg_init(des_con, src, dst);
  348. nbytes = des_con->nbytes;
  349. cipher = crypto_ablkcipher_reqtfm(areq);
  350. ctx = crypto_ablkcipher_ctx(cipher);
  351. if (des_queue->hw_status == DES_IDLE) {
  352. des_queue->hw_status = DES_STARTED;
  353. }
  354. des_con->bytes_processed -= des_con->nbytes;
  355. err = ablkcipher_enqueue_request(&des_queue->list, &des_con->arequest);
  356. if (err == -EBUSY) {
  357. printk("Failed to enqueue request, ln: %d, err: %d\n",
  358. __LINE__, err);
  359. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  360. return -EINVAL;
  361. }
  362. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  363. err = lq_deu_des_core(ctx, des_con->dst_buf, des_con->src_buf, iv, nbytes, encdec, mode);
  364. return err;
  365. }
  366. /* \fn static void process_queue(unsigned long data)
  367. * \ingroup IFX_DES_FUNCTIONS
  368. * \brief Process next packet in queue
  369. * \param data not used
  370. * \return
  371. */
  372. static void process_queue(unsigned long data)
  373. {
  374. DEU_WAKEUP_EVENT(deu_dma_priv.deu_thread_wait, DES_ASYNC_EVENT,
  375. deu_dma_priv.des_event_flags);
  376. }
  377. /* \fn static int des_crypto_thread (void *data)
  378. * \ingroup IFX_DES_FUNCTIONS
  379. * \brief DES thread that handles crypto requests from upper layer & DMA
  380. * \param *data Not used
  381. * \return -EINVAL: DEU failure, -EBUSY: DEU HW busy, 0: exit thread
  382. */
  383. static int des_crypto_thread(void *data)
  384. {
  385. struct des_container *des_con = NULL;
  386. struct ablkcipher_request *areq = NULL;
  387. int err;
  388. unsigned long queue_flag;
  389. daemonize("lq_des_thread");
  390. while (1)
  391. {
  392. DEU_WAIT_EVENT(deu_dma_priv.deu_thread_wait, DES_ASYNC_EVENT,
  393. deu_dma_priv.des_event_flags);
  394. spin_lock_irqsave(&des_queue->lock, queue_flag);
  395. /* wait to prevent starting a crypto session before
  396. * exiting the dma interrupt thread.
  397. */
  398. if (des_queue->hw_status == DES_STARTED) {
  399. areq = ablkcipher_dequeue_request(&des_queue->list);
  400. des_con = des_container_cast(areq);
  401. des_queue->hw_status = DES_BUSY;
  402. }
  403. else if (des_queue->hw_status == DES_IDLE) {
  404. areq = ablkcipher_dequeue_request(&des_queue->list);
  405. des_con = des_container_cast(areq);
  406. des_queue->hw_status = DES_STARTED;
  407. }
  408. else if (des_queue->hw_status == DES_BUSY) {
  409. areq = ablkcipher_dequeue_request(&des_queue->list);
  410. des_con = des_container_cast(areq);
  411. }
  412. else if (des_queue->hw_status == DES_COMPLETED) {
  413. areq->base.complete(&areq->base, 0);
  414. lq_sg_complete(des_con);
  415. des_queue->hw_status = DES_IDLE;
  416. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  417. return 0;
  418. }
  419. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  420. if ((des_con->bytes_processed == 0)) {
  421. goto des_done;
  422. }
  423. if (!des_con) {
  424. goto des_done;
  425. }
  426. if (des_con->flag & PROCESS_NEW_PACKET) {
  427. des_con->flag = PROCESS_SCATTER;
  428. err = process_next_packet(des_con, areq, PROCESS_NEW_PACKET);
  429. }
  430. else
  431. err = process_next_packet(des_con, areq, PROCESS_SCATTER);
  432. if (err == -EINVAL) {
  433. areq->base.complete(&areq->base, err);
  434. lq_sg_complete(des_con);
  435. printk("src/dst returned -EINVAL in func: %s\n", __func__);
  436. }
  437. else if (err > 0) {
  438. printk("src/dst returned zero in func: %s\n", __func__);
  439. goto des_done;
  440. }
  441. continue;
  442. des_done:
  443. //printk("debug line - %d, func: %s, qlen: %d\n", __LINE__, __func__, des_queue->list.qlen);
  444. areq->base.complete(&areq->base, 0);
  445. lq_sg_complete(des_con);
  446. spin_lock_irqsave(&des_queue->lock, queue_flag);
  447. if (des_queue->list.qlen > 0) {
  448. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  449. tasklet_schedule(&des_queue->des_task);
  450. }
  451. else {
  452. des_queue->hw_status = DES_IDLE;
  453. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  454. }
  455. } // while(1)
  456. return 0;
  457. }
  458. /* \fn static int lq_des_queue_mgr(struct des_ctx *ctx, struct ablkcipher_request *areq,
  459. u8 *iv, int encdec, int mode)
  460. * \ingroup IFX_DES_FUNCTIONS
  461. * \brief starts the process of queuing DEU requests
  462. * \param *ctx crypto algo contax
  463. * \param *areq Pointer to the balkcipher requests
  464. * \param *iv Pointer to intput vector location
  465. * \param dir Encrypt/Decrypt
  466. * \mode The mode DES algo is running
  467. * \return 0 if success
  468. */
  469. static int lq_queue_mgr(struct des_ctx *ctx, struct ablkcipher_request *areq,
  470. u8 *iv, int encdec, int mode)
  471. {
  472. int err = -EINVAL;
  473. unsigned long queue_flag;
  474. struct scatterlist *src = areq->src;
  475. struct scatterlist *dst = areq->dst;
  476. struct des_container *des_con = NULL;
  477. u32 remain, inc, nbytes = areq->nbytes;
  478. u32 chunk_bytes = src->length;
  479. des_con = (struct des_container *)kmalloc(sizeof(struct des_container),
  480. GFP_KERNEL);
  481. if (!(des_con)) {
  482. printk("Cannot allocate memory for AES container, fn %s, ln %d\n",
  483. __func__, __LINE__);
  484. return -ENOMEM;
  485. }
  486. /* DES encrypt/decrypt mode */
  487. if (mode == 5) {
  488. nbytes = DES_BLOCK_SIZE;
  489. chunk_bytes = DES_BLOCK_SIZE;
  490. mode = 0;
  491. }
  492. des_con->bytes_processed = nbytes;
  493. des_con->arequest = (*areq);
  494. remain = nbytes;
  495. //printk("debug - Line: %d, func: %s, reqsize: %d, scattersize: %d\n",
  496. // __LINE__, __func__, nbytes, chunk_bytes);
  497. if (remain > DEU_MAX_PACKET_SIZE)
  498. inc = DEU_MAX_PACKET_SIZE;
  499. else if(remain > chunk_bytes)
  500. inc = chunk_bytes;
  501. else
  502. inc = remain;
  503. remain -= inc;
  504. lq_sg_init(des_con, src, dst);
  505. if (remain <= 0 ) {
  506. des_con->complete = 1;
  507. }
  508. else
  509. des_con->complete = 0;
  510. des_con->nbytes = inc;
  511. des_con->iv = iv;
  512. des_con->mode = mode;
  513. des_con->encdec = encdec;
  514. spin_lock_irqsave(&des_queue->lock, queue_flag);
  515. if (des_queue->hw_status == DES_STARTED || des_queue->hw_status == DES_BUSY ||
  516. des_queue->list.qlen > 0) {
  517. des_con->flag = PROCESS_NEW_PACKET;
  518. err = ablkcipher_enqueue_request(&des_queue->list, &des_con->arequest);
  519. if (err == -EBUSY) {
  520. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  521. printk("Fail to enqueue ablkcipher request ln: %d, err: %d\n",
  522. __LINE__, err);
  523. return err;
  524. }
  525. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  526. return -EINPROGRESS;
  527. }
  528. else if (des_queue->hw_status == DES_IDLE) {
  529. des_queue->hw_status = DES_STARTED;
  530. }
  531. des_con->flag = PROCESS_SCATTER;
  532. des_con->bytes_processed -= des_con->nbytes;
  533. err = ablkcipher_enqueue_request(&des_queue->list, &des_con->arequest);
  534. if (err == -EBUSY) {
  535. printk("Fail to enqueue ablkcipher request ln: %d, err: %d\n",
  536. __LINE__, err);
  537. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  538. return err;
  539. }
  540. spin_unlock_irqrestore(&des_queue->lock, queue_flag);
  541. return lq_deu_des_core(ctx, des_con->dst_buf, des_con->src_buf, iv, inc, encdec, mode);
  542. }
  543. /* \fn static int lq_des_encrypt(struct ablkcipher_request *areq)
  544. * \ingroup IFX_DES_FUNCTIONS
  545. * \brief Decrypt function for DES algo
  546. * \param *areq Pointer to ablkcipher request in memory
  547. * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
  548. */
  549. static int lq_des_encrypt(struct ablkcipher_request *areq)
  550. {
  551. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  552. struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  553. return lq_queue_mgr(ctx, areq, NULL, CRYPTO_DIR_ENCRYPT, 5);
  554. }
  555. /* \fn static int lq_des_decrypt(struct ablkcipher_request *areq)
  556. * \ingroup IFX_DES_FUNCTIONS
  557. * \brief Decrypt function for DES algo
  558. * \param *areq Pointer to ablkcipher request in memory
  559. * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
  560. */
  561. static int lq_des_decrypt(struct ablkcipher_request *areq)
  562. {
  563. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  564. struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  565. return lq_queue_mgr(ctx, areq, NULL, CRYPTO_DIR_DECRYPT, 5);
  566. }
  567. /* \fn static int lq_ecb_des_encrypt(struct ablkcipher_request *areq)
  568. * \ingroup IFX_DES_FUNCTIONS
  569. * \brief Decrypt function for DES algo
  570. * \param *areq Pointer to ablkcipher request in memory
  571. * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
  572. */
  573. static int lq_ecb_des_encrypt(struct ablkcipher_request *areq)
  574. {
  575. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  576. struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  577. return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_ENCRYPT, 0);
  578. }
  579. /* \fn static int lq_ecb_des_decrypt(struct ablkcipher_request *areq)
  580. * \ingroup IFX_DES_FUNCTIONS
  581. * \brief Decrypt function for DES algo
  582. * \param *areq Pointer to ablkcipher request in memory
  583. * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
  584. */
  585. static int lq_ecb_des_decrypt(struct ablkcipher_request *areq)
  586. {
  587. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  588. struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  589. return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_DECRYPT, 0);
  590. }
  591. /* \fn static int lq_cbc_ecb_des_encrypt(struct ablkcipher_request *areq)
  592. * \ingroup IFX_DES_FUNCTIONS
  593. * \brief Decrypt function for DES algo
  594. * \param *areq Pointer to ablkcipher request in memory
  595. * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
  596. */
  597. static int lq_cbc_des_encrypt(struct ablkcipher_request *areq)
  598. {
  599. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  600. struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  601. return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_ENCRYPT, 1);
  602. }
  603. /* \fn static int lq_cbc_des_decrypt(struct ablkcipher_request *areq)
  604. * \ingroup IFX_DES_FUNCTIONS
  605. * \brief Decrypt function for DES algo
  606. * \param *areq Pointer to ablkcipher request in memory
  607. * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
  608. */
  609. static int lq_cbc_des_decrypt(struct ablkcipher_request *areq)
  610. {
  611. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  612. struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  613. return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_DECRYPT, 1);
  614. }
  615. struct lq_des_alg {
  616. struct crypto_alg alg;
  617. };
  618. /* DES Supported algo array */
  619. static struct lq_des_alg des_drivers_alg [] = {
  620. {
  621. .alg = {
  622. .cra_name = "des",
  623. .cra_driver_name = "lqdeu-des",
  624. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  625. .cra_blocksize = DES_BLOCK_SIZE,
  626. .cra_ctxsize = sizeof(struct des_ctx),
  627. .cra_type = &crypto_ablkcipher_type,
  628. .cra_priority = 300,
  629. .cra_module = THIS_MODULE,
  630. .cra_ablkcipher = {
  631. .setkey = lq_des_setkey,
  632. .encrypt = lq_des_encrypt,
  633. .decrypt = lq_des_decrypt,
  634. .geniv = "eseqiv",
  635. .min_keysize = DES_KEY_SIZE,
  636. .max_keysize = DES_KEY_SIZE,
  637. .ivsize = DES_BLOCK_SIZE,
  638. }
  639. }
  640. },{
  641. .alg = {
  642. .cra_name = "ecb(des)",
  643. .cra_driver_name = "lqdeu-ecb(des)",
  644. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  645. .cra_blocksize = DES_BLOCK_SIZE,
  646. .cra_ctxsize = sizeof(struct des_ctx),
  647. .cra_type = &crypto_ablkcipher_type,
  648. .cra_priority = 300,
  649. .cra_module = THIS_MODULE,
  650. .cra_ablkcipher = {
  651. .setkey = lq_des_setkey,
  652. .encrypt = lq_ecb_des_encrypt,
  653. .decrypt = lq_ecb_des_decrypt,
  654. .geniv = "eseqiv",
  655. .min_keysize = DES_KEY_SIZE,
  656. .max_keysize = DES_KEY_SIZE,
  657. .ivsize = DES_BLOCK_SIZE,
  658. }
  659. }
  660. },{
  661. .alg = {
  662. .cra_name = "cbc(des)",
  663. .cra_driver_name = "lqdeu-cbc(des)",
  664. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  665. .cra_blocksize = DES_BLOCK_SIZE,
  666. .cra_ctxsize = sizeof(struct des_ctx),
  667. .cra_type = &crypto_ablkcipher_type,
  668. .cra_priority = 300,
  669. .cra_module = THIS_MODULE,
  670. .cra_ablkcipher = {
  671. .setkey = lq_des_setkey,
  672. .encrypt = lq_cbc_des_encrypt,
  673. .decrypt = lq_cbc_des_decrypt,
  674. .geniv = "eseqiv",
  675. .min_keysize = DES3_EDE_KEY_SIZE,
  676. .max_keysize = DES3_EDE_KEY_SIZE,
  677. .ivsize = DES3_EDE_BLOCK_SIZE,
  678. }
  679. }
  680. },{
  681. .alg = {
  682. .cra_name = "des3_ede",
  683. .cra_driver_name = "lqdeu-des3_ede",
  684. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  685. .cra_blocksize = DES_BLOCK_SIZE,
  686. .cra_ctxsize = sizeof(struct des_ctx),
  687. .cra_type = &crypto_ablkcipher_type,
  688. .cra_priority = 300,
  689. .cra_module = THIS_MODULE,
  690. .cra_ablkcipher = {
  691. .setkey = lq_des3_ede_setkey,
  692. .encrypt = lq_des_encrypt,
  693. .decrypt = lq_des_decrypt,
  694. .geniv = "eseqiv",
  695. .min_keysize = DES_KEY_SIZE,
  696. .max_keysize = DES_KEY_SIZE,
  697. .ivsize = DES_BLOCK_SIZE,
  698. }
  699. }
  700. },{
  701. .alg = {
  702. .cra_name = "ecb(des3_ede)",
  703. .cra_driver_name = "lqdeu-ecb(des3_ede)",
  704. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  705. .cra_blocksize = DES_BLOCK_SIZE,
  706. .cra_ctxsize = sizeof(struct des_ctx),
  707. .cra_type = &crypto_ablkcipher_type,
  708. .cra_priority = 300,
  709. .cra_module = THIS_MODULE,
  710. .cra_ablkcipher = {
  711. .setkey = lq_des3_ede_setkey,
  712. .encrypt = lq_ecb_des_encrypt,
  713. .decrypt = lq_ecb_des_decrypt,
  714. .geniv = "eseqiv",
  715. .min_keysize = DES3_EDE_KEY_SIZE,
  716. .max_keysize = DES3_EDE_KEY_SIZE,
  717. .ivsize = DES3_EDE_BLOCK_SIZE,
  718. }
  719. }
  720. },{
  721. .alg = {
  722. .cra_name = "cbc(des3_ede)",
  723. .cra_driver_name = "lqdeu-cbc(des3_ede)",
  724. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  725. .cra_blocksize = DES_BLOCK_SIZE,
  726. .cra_ctxsize = sizeof(struct des_ctx),
  727. .cra_type = &crypto_ablkcipher_type,
  728. .cra_priority = 300,
  729. .cra_module = THIS_MODULE,
  730. .cra_ablkcipher = {
  731. .setkey = lq_des3_ede_setkey,
  732. .encrypt = lq_cbc_des_encrypt,
  733. .decrypt = lq_cbc_des_decrypt,
  734. .geniv = "eseqiv",
  735. .min_keysize = DES3_EDE_KEY_SIZE,
  736. .max_keysize = DES3_EDE_KEY_SIZE,
  737. .ivsize = DES3_EDE_BLOCK_SIZE,
  738. }
  739. }
  740. }
  741. };
  742. /*! \fn int __init lqdeu_async_des_init (void)
  743. * \ingroup IFX_DES_FUNCTIONS
  744. * \brief initialize des driver
  745. */
  746. int __init lqdeu_async_des_init (void)
  747. {
  748. int i, j, ret = -EINVAL;
  749. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
  750. if (!disable_multiblock) {
  751. ifxdeu_des_alg.cra_u.cipher.cia_max_nbytes = DES_BLOCK_SIZE; //(size_t)-1;
  752. ifxdeu_des_alg.cra_u.cipher.cia_req_align = 16;
  753. ifxdeu_des_alg.cra_u.cipher.cia_ecb = ifx_deu_des_ecb;
  754. ifxdeu_des_alg.cra_u.cipher.cia_cbc = ifx_deu_des_cbc;
  755. ifxdeu_des_alg.cra_u.cipher.cia_cfb = ifx_deu_des_cfb;
  756. ifxdeu_des_alg.cra_u.cipher.cia_ofb = ifx_deu_des_ofb;
  757. }
  758. #endif
  759. for (i = 0; i < ARRAY_SIZE(des_drivers_alg); i++) {
  760. ret = crypto_register_alg(&des_drivers_alg[i].alg);
  761. //printk("driver: %s\n", des_drivers_alg[i].alg.cra_name);
  762. if (ret)
  763. goto des_err;
  764. }
  765. des_chip_init();
  766. CRTCL_SECT_INIT;
  767. printk (KERN_NOTICE "IFX DEU DES initialized%s%s.\n", disable_multiblock ? "" : " (multiblock)", disable_deudma ? "" : " (DMA)");
  768. return ret;
  769. des_err:
  770. for (j = 0; j < i; j++)
  771. crypto_unregister_alg(&des_drivers_alg[i].alg);
  772. printk(KERN_ERR "Lantiq %s driver initialization failed!\n", (char *)&des_drivers_alg[i].alg.cra_driver_name);
  773. return ret;
  774. cbc_des3_ede_err:
  775. for (i = 0; i < ARRAY_SIZE(des_drivers_alg); i++) {
  776. if (!strcmp((char *)&des_drivers_alg[i].alg.cra_name, "cbc(des3_ede)"))
  777. crypto_unregister_alg(&des_drivers_alg[i].alg);
  778. }
  779. printk(KERN_ERR "Lantiq %s driver initialization failed!\n", (char *)&des_drivers_alg[i].alg.cra_driver_name);
  780. return ret;
  781. }
  782. /*! \fn void __exit lqdeu_fini_async_des (void)
  783. * \ingroup IFX_DES_FUNCTIONS
  784. * \brief unregister des driver
  785. */
  786. void __exit lqdeu_fini_async_des (void)
  787. {
  788. int i;
  789. for (i = 0; i < ARRAY_SIZE(des_drivers_alg); i++)
  790. crypto_unregister_alg(&des_drivers_alg[i].alg);
  791. des_queue->hw_status = DES_COMPLETED;
  792. DEU_WAKEUP_EVENT(deu_dma_priv.deu_thread_wait, DES_ASYNC_EVENT,
  793. deu_dma_priv.des_event_flags);
  794. kfree(des_queue);
  795. }