crypto4xx_core.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. /**
  2. * AMCC SoC PPC4xx Crypto Driver
  3. *
  4. * Copyright (c) 2008 Applied Micro Circuits Corporation.
  5. * All rights reserved. James Hsiao <jhsiao@amcc.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 as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * This file implements AMCC crypto offload Linux device driver for use with
  18. * Linux CryptoAPI.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/spinlock_types.h>
  23. #include <linux/random.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/crypto.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/init.h>
  29. #include <linux/of_platform.h>
  30. #include <asm/dcr.h>
  31. #include <asm/dcr-regs.h>
  32. #include <asm/cacheflush.h>
  33. #include <crypto/aes.h>
  34. #include <crypto/sha.h>
  35. #include "crypto4xx_reg_def.h"
  36. #include "crypto4xx_core.h"
  37. #include "crypto4xx_sa.h"
  38. #define PPC4XX_SEC_VERSION_STR "0.5"
  39. /**
  40. * PPC4xx Crypto Engine Initialization Routine
  41. */
  42. static void crypto4xx_hw_init(struct crypto4xx_device *dev)
  43. {
  44. union ce_ring_size ring_size;
  45. union ce_ring_contol ring_ctrl;
  46. union ce_part_ring_size part_ring_size;
  47. union ce_io_threshold io_threshold;
  48. u32 rand_num;
  49. union ce_pe_dma_cfg pe_dma_cfg;
  50. writel(PPC4XX_BYTE_ORDER, dev->ce_base + CRYPTO4XX_BYTE_ORDER_CFG);
  51. /* setup pe dma, include reset sg, pdr and pe, then release reset */
  52. pe_dma_cfg.w = 0;
  53. pe_dma_cfg.bf.bo_sgpd_en = 1;
  54. pe_dma_cfg.bf.bo_data_en = 0;
  55. pe_dma_cfg.bf.bo_sa_en = 1;
  56. pe_dma_cfg.bf.bo_pd_en = 1;
  57. pe_dma_cfg.bf.dynamic_sa_en = 1;
  58. pe_dma_cfg.bf.reset_sg = 1;
  59. pe_dma_cfg.bf.reset_pdr = 1;
  60. pe_dma_cfg.bf.reset_pe = 1;
  61. writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
  62. /* un reset pe,sg and pdr */
  63. pe_dma_cfg.bf.pe_mode = 0;
  64. pe_dma_cfg.bf.reset_sg = 0;
  65. pe_dma_cfg.bf.reset_pdr = 0;
  66. pe_dma_cfg.bf.reset_pe = 0;
  67. pe_dma_cfg.bf.bo_td_en = 0;
  68. writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
  69. writel(dev->pdr_pa, dev->ce_base + CRYPTO4XX_PDR_BASE);
  70. writel(dev->pdr_pa, dev->ce_base + CRYPTO4XX_RDR_BASE);
  71. writel(PPC4XX_PRNG_CTRL_AUTO_EN, dev->ce_base + CRYPTO4XX_PRNG_CTRL);
  72. get_random_bytes(&rand_num, sizeof(rand_num));
  73. writel(rand_num, dev->ce_base + CRYPTO4XX_PRNG_SEED_L);
  74. get_random_bytes(&rand_num, sizeof(rand_num));
  75. writel(rand_num, dev->ce_base + CRYPTO4XX_PRNG_SEED_H);
  76. ring_size.w = 0;
  77. ring_size.bf.ring_offset = PPC4XX_PD_SIZE;
  78. ring_size.bf.ring_size = PPC4XX_NUM_PD;
  79. writel(ring_size.w, dev->ce_base + CRYPTO4XX_RING_SIZE);
  80. ring_ctrl.w = 0;
  81. writel(ring_ctrl.w, dev->ce_base + CRYPTO4XX_RING_CTRL);
  82. writel(PPC4XX_DC_3DES_EN, dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
  83. writel(dev->gdr_pa, dev->ce_base + CRYPTO4XX_GATH_RING_BASE);
  84. writel(dev->sdr_pa, dev->ce_base + CRYPTO4XX_SCAT_RING_BASE);
  85. part_ring_size.w = 0;
  86. part_ring_size.bf.sdr_size = PPC4XX_SDR_SIZE;
  87. part_ring_size.bf.gdr_size = PPC4XX_GDR_SIZE;
  88. writel(part_ring_size.w, dev->ce_base + CRYPTO4XX_PART_RING_SIZE);
  89. writel(PPC4XX_SD_BUFFER_SIZE, dev->ce_base + CRYPTO4XX_PART_RING_CFG);
  90. io_threshold.w = 0;
  91. io_threshold.bf.output_threshold = PPC4XX_OUTPUT_THRESHOLD;
  92. io_threshold.bf.input_threshold = PPC4XX_INPUT_THRESHOLD;
  93. writel(io_threshold.w, dev->ce_base + CRYPTO4XX_IO_THRESHOLD);
  94. writel(0, dev->ce_base + CRYPTO4XX_PDR_BASE_UADDR);
  95. writel(0, dev->ce_base + CRYPTO4XX_RDR_BASE_UADDR);
  96. writel(0, dev->ce_base + CRYPTO4XX_PKT_SRC_UADDR);
  97. writel(0, dev->ce_base + CRYPTO4XX_PKT_DEST_UADDR);
  98. writel(0, dev->ce_base + CRYPTO4XX_SA_UADDR);
  99. writel(0, dev->ce_base + CRYPTO4XX_GATH_RING_BASE_UADDR);
  100. writel(0, dev->ce_base + CRYPTO4XX_SCAT_RING_BASE_UADDR);
  101. /* un reset pe,sg and pdr */
  102. pe_dma_cfg.bf.pe_mode = 1;
  103. pe_dma_cfg.bf.reset_sg = 0;
  104. pe_dma_cfg.bf.reset_pdr = 0;
  105. pe_dma_cfg.bf.reset_pe = 0;
  106. pe_dma_cfg.bf.bo_td_en = 0;
  107. writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
  108. /*clear all pending interrupt*/
  109. writel(PPC4XX_INTERRUPT_CLR, dev->ce_base + CRYPTO4XX_INT_CLR);
  110. writel(PPC4XX_INT_DESCR_CNT, dev->ce_base + CRYPTO4XX_INT_DESCR_CNT);
  111. writel(PPC4XX_INT_DESCR_CNT, dev->ce_base + CRYPTO4XX_INT_DESCR_CNT);
  112. writel(PPC4XX_INT_CFG, dev->ce_base + CRYPTO4XX_INT_CFG);
  113. writel(PPC4XX_PD_DONE_INT, dev->ce_base + CRYPTO4XX_INT_EN);
  114. }
  115. int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size)
  116. {
  117. ctx->sa_in = dma_alloc_coherent(ctx->dev->core_dev->device, size * 4,
  118. &ctx->sa_in_dma_addr, GFP_ATOMIC);
  119. if (ctx->sa_in == NULL)
  120. return -ENOMEM;
  121. ctx->sa_out = dma_alloc_coherent(ctx->dev->core_dev->device, size * 4,
  122. &ctx->sa_out_dma_addr, GFP_ATOMIC);
  123. if (ctx->sa_out == NULL) {
  124. dma_free_coherent(ctx->dev->core_dev->device,
  125. ctx->sa_len * 4,
  126. ctx->sa_in, ctx->sa_in_dma_addr);
  127. return -ENOMEM;
  128. }
  129. memset(ctx->sa_in, 0, size * 4);
  130. memset(ctx->sa_out, 0, size * 4);
  131. ctx->sa_len = size;
  132. return 0;
  133. }
  134. void crypto4xx_free_sa(struct crypto4xx_ctx *ctx)
  135. {
  136. if (ctx->sa_in != NULL)
  137. dma_free_coherent(ctx->dev->core_dev->device, ctx->sa_len * 4,
  138. ctx->sa_in, ctx->sa_in_dma_addr);
  139. if (ctx->sa_out != NULL)
  140. dma_free_coherent(ctx->dev->core_dev->device, ctx->sa_len * 4,
  141. ctx->sa_out, ctx->sa_out_dma_addr);
  142. ctx->sa_in_dma_addr = 0;
  143. ctx->sa_out_dma_addr = 0;
  144. ctx->sa_len = 0;
  145. }
  146. u32 crypto4xx_alloc_state_record(struct crypto4xx_ctx *ctx)
  147. {
  148. ctx->state_record = dma_alloc_coherent(ctx->dev->core_dev->device,
  149. sizeof(struct sa_state_record),
  150. &ctx->state_record_dma_addr, GFP_ATOMIC);
  151. if (!ctx->state_record_dma_addr)
  152. return -ENOMEM;
  153. memset(ctx->state_record, 0, sizeof(struct sa_state_record));
  154. return 0;
  155. }
  156. void crypto4xx_free_state_record(struct crypto4xx_ctx *ctx)
  157. {
  158. if (ctx->state_record != NULL)
  159. dma_free_coherent(ctx->dev->core_dev->device,
  160. sizeof(struct sa_state_record),
  161. ctx->state_record,
  162. ctx->state_record_dma_addr);
  163. ctx->state_record_dma_addr = 0;
  164. }
  165. /**
  166. * alloc memory for the gather ring
  167. * no need to alloc buf for the ring
  168. * gdr_tail, gdr_head and gdr_count are initialized by this function
  169. */
  170. static u32 crypto4xx_build_pdr(struct crypto4xx_device *dev)
  171. {
  172. int i;
  173. struct pd_uinfo *pd_uinfo;
  174. dev->pdr = dma_alloc_coherent(dev->core_dev->device,
  175. sizeof(struct ce_pd) * PPC4XX_NUM_PD,
  176. &dev->pdr_pa, GFP_ATOMIC);
  177. if (!dev->pdr)
  178. return -ENOMEM;
  179. dev->pdr_uinfo = kzalloc(sizeof(struct pd_uinfo) * PPC4XX_NUM_PD,
  180. GFP_KERNEL);
  181. if (!dev->pdr_uinfo) {
  182. dma_free_coherent(dev->core_dev->device,
  183. sizeof(struct ce_pd) * PPC4XX_NUM_PD,
  184. dev->pdr,
  185. dev->pdr_pa);
  186. return -ENOMEM;
  187. }
  188. memset(dev->pdr, 0, sizeof(struct ce_pd) * PPC4XX_NUM_PD);
  189. dev->shadow_sa_pool = dma_alloc_coherent(dev->core_dev->device,
  190. 256 * PPC4XX_NUM_PD,
  191. &dev->shadow_sa_pool_pa,
  192. GFP_ATOMIC);
  193. if (!dev->shadow_sa_pool)
  194. return -ENOMEM;
  195. dev->shadow_sr_pool = dma_alloc_coherent(dev->core_dev->device,
  196. sizeof(struct sa_state_record) * PPC4XX_NUM_PD,
  197. &dev->shadow_sr_pool_pa, GFP_ATOMIC);
  198. if (!dev->shadow_sr_pool)
  199. return -ENOMEM;
  200. for (i = 0; i < PPC4XX_NUM_PD; i++) {
  201. pd_uinfo = (struct pd_uinfo *) (dev->pdr_uinfo +
  202. sizeof(struct pd_uinfo) * i);
  203. /* alloc 256 bytes which is enough for any kind of dynamic sa */
  204. pd_uinfo->sa_va = dev->shadow_sa_pool + 256 * i;
  205. pd_uinfo->sa_pa = dev->shadow_sa_pool_pa + 256 * i;
  206. /* alloc state record */
  207. pd_uinfo->sr_va = dev->shadow_sr_pool +
  208. sizeof(struct sa_state_record) * i;
  209. pd_uinfo->sr_pa = dev->shadow_sr_pool_pa +
  210. sizeof(struct sa_state_record) * i;
  211. }
  212. return 0;
  213. }
  214. static void crypto4xx_destroy_pdr(struct crypto4xx_device *dev)
  215. {
  216. if (dev->pdr != NULL)
  217. dma_free_coherent(dev->core_dev->device,
  218. sizeof(struct ce_pd) * PPC4XX_NUM_PD,
  219. dev->pdr, dev->pdr_pa);
  220. if (dev->shadow_sa_pool)
  221. dma_free_coherent(dev->core_dev->device, 256 * PPC4XX_NUM_PD,
  222. dev->shadow_sa_pool, dev->shadow_sa_pool_pa);
  223. if (dev->shadow_sr_pool)
  224. dma_free_coherent(dev->core_dev->device,
  225. sizeof(struct sa_state_record) * PPC4XX_NUM_PD,
  226. dev->shadow_sr_pool, dev->shadow_sr_pool_pa);
  227. kfree(dev->pdr_uinfo);
  228. }
  229. static u32 crypto4xx_get_pd_from_pdr_nolock(struct crypto4xx_device *dev)
  230. {
  231. u32 retval;
  232. u32 tmp;
  233. retval = dev->pdr_head;
  234. tmp = (dev->pdr_head + 1) % PPC4XX_NUM_PD;
  235. if (tmp == dev->pdr_tail)
  236. return ERING_WAS_FULL;
  237. dev->pdr_head = tmp;
  238. return retval;
  239. }
  240. static u32 crypto4xx_put_pd_to_pdr(struct crypto4xx_device *dev, u32 idx)
  241. {
  242. struct pd_uinfo *pd_uinfo;
  243. unsigned long flags;
  244. pd_uinfo = (struct pd_uinfo *)(dev->pdr_uinfo +
  245. sizeof(struct pd_uinfo) * idx);
  246. spin_lock_irqsave(&dev->core_dev->lock, flags);
  247. if (dev->pdr_tail != PPC4XX_LAST_PD)
  248. dev->pdr_tail++;
  249. else
  250. dev->pdr_tail = 0;
  251. pd_uinfo->state = PD_ENTRY_FREE;
  252. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  253. return 0;
  254. }
  255. static struct ce_pd *crypto4xx_get_pdp(struct crypto4xx_device *dev,
  256. dma_addr_t *pd_dma, u32 idx)
  257. {
  258. *pd_dma = dev->pdr_pa + sizeof(struct ce_pd) * idx;
  259. return dev->pdr + sizeof(struct ce_pd) * idx;
  260. }
  261. /**
  262. * alloc memory for the gather ring
  263. * no need to alloc buf for the ring
  264. * gdr_tail, gdr_head and gdr_count are initialized by this function
  265. */
  266. static u32 crypto4xx_build_gdr(struct crypto4xx_device *dev)
  267. {
  268. dev->gdr = dma_alloc_coherent(dev->core_dev->device,
  269. sizeof(struct ce_gd) * PPC4XX_NUM_GD,
  270. &dev->gdr_pa, GFP_ATOMIC);
  271. if (!dev->gdr)
  272. return -ENOMEM;
  273. memset(dev->gdr, 0, sizeof(struct ce_gd) * PPC4XX_NUM_GD);
  274. return 0;
  275. }
  276. static inline void crypto4xx_destroy_gdr(struct crypto4xx_device *dev)
  277. {
  278. dma_free_coherent(dev->core_dev->device,
  279. sizeof(struct ce_gd) * PPC4XX_NUM_GD,
  280. dev->gdr, dev->gdr_pa);
  281. }
  282. /*
  283. * when this function is called.
  284. * preemption or interrupt must be disabled
  285. */
  286. u32 crypto4xx_get_n_gd(struct crypto4xx_device *dev, int n)
  287. {
  288. u32 retval;
  289. u32 tmp;
  290. if (n >= PPC4XX_NUM_GD)
  291. return ERING_WAS_FULL;
  292. retval = dev->gdr_head;
  293. tmp = (dev->gdr_head + n) % PPC4XX_NUM_GD;
  294. if (dev->gdr_head > dev->gdr_tail) {
  295. if (tmp < dev->gdr_head && tmp >= dev->gdr_tail)
  296. return ERING_WAS_FULL;
  297. } else if (dev->gdr_head < dev->gdr_tail) {
  298. if (tmp < dev->gdr_head || tmp >= dev->gdr_tail)
  299. return ERING_WAS_FULL;
  300. }
  301. dev->gdr_head = tmp;
  302. return retval;
  303. }
  304. static u32 crypto4xx_put_gd_to_gdr(struct crypto4xx_device *dev)
  305. {
  306. unsigned long flags;
  307. spin_lock_irqsave(&dev->core_dev->lock, flags);
  308. if (dev->gdr_tail == dev->gdr_head) {
  309. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  310. return 0;
  311. }
  312. if (dev->gdr_tail != PPC4XX_LAST_GD)
  313. dev->gdr_tail++;
  314. else
  315. dev->gdr_tail = 0;
  316. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  317. return 0;
  318. }
  319. static inline struct ce_gd *crypto4xx_get_gdp(struct crypto4xx_device *dev,
  320. dma_addr_t *gd_dma, u32 idx)
  321. {
  322. *gd_dma = dev->gdr_pa + sizeof(struct ce_gd) * idx;
  323. return (struct ce_gd *) (dev->gdr + sizeof(struct ce_gd) * idx);
  324. }
  325. /**
  326. * alloc memory for the scatter ring
  327. * need to alloc buf for the ring
  328. * sdr_tail, sdr_head and sdr_count are initialized by this function
  329. */
  330. static u32 crypto4xx_build_sdr(struct crypto4xx_device *dev)
  331. {
  332. int i;
  333. struct ce_sd *sd_array;
  334. /* alloc memory for scatter descriptor ring */
  335. dev->sdr = dma_alloc_coherent(dev->core_dev->device,
  336. sizeof(struct ce_sd) * PPC4XX_NUM_SD,
  337. &dev->sdr_pa, GFP_ATOMIC);
  338. if (!dev->sdr)
  339. return -ENOMEM;
  340. dev->scatter_buffer_size = PPC4XX_SD_BUFFER_SIZE;
  341. dev->scatter_buffer_va =
  342. dma_alloc_coherent(dev->core_dev->device,
  343. dev->scatter_buffer_size * PPC4XX_NUM_SD,
  344. &dev->scatter_buffer_pa, GFP_ATOMIC);
  345. if (!dev->scatter_buffer_va) {
  346. dma_free_coherent(dev->core_dev->device,
  347. sizeof(struct ce_sd) * PPC4XX_NUM_SD,
  348. dev->sdr, dev->sdr_pa);
  349. return -ENOMEM;
  350. }
  351. sd_array = dev->sdr;
  352. for (i = 0; i < PPC4XX_NUM_SD; i++) {
  353. sd_array[i].ptr = dev->scatter_buffer_pa +
  354. dev->scatter_buffer_size * i;
  355. }
  356. return 0;
  357. }
  358. static void crypto4xx_destroy_sdr(struct crypto4xx_device *dev)
  359. {
  360. if (dev->sdr != NULL)
  361. dma_free_coherent(dev->core_dev->device,
  362. sizeof(struct ce_sd) * PPC4XX_NUM_SD,
  363. dev->sdr, dev->sdr_pa);
  364. if (dev->scatter_buffer_va != NULL)
  365. dma_free_coherent(dev->core_dev->device,
  366. dev->scatter_buffer_size * PPC4XX_NUM_SD,
  367. dev->scatter_buffer_va,
  368. dev->scatter_buffer_pa);
  369. }
  370. /*
  371. * when this function is called.
  372. * preemption or interrupt must be disabled
  373. */
  374. static u32 crypto4xx_get_n_sd(struct crypto4xx_device *dev, int n)
  375. {
  376. u32 retval;
  377. u32 tmp;
  378. if (n >= PPC4XX_NUM_SD)
  379. return ERING_WAS_FULL;
  380. retval = dev->sdr_head;
  381. tmp = (dev->sdr_head + n) % PPC4XX_NUM_SD;
  382. if (dev->sdr_head > dev->gdr_tail) {
  383. if (tmp < dev->sdr_head && tmp >= dev->sdr_tail)
  384. return ERING_WAS_FULL;
  385. } else if (dev->sdr_head < dev->sdr_tail) {
  386. if (tmp < dev->sdr_head || tmp >= dev->sdr_tail)
  387. return ERING_WAS_FULL;
  388. } /* the head = tail, or empty case is already take cared */
  389. dev->sdr_head = tmp;
  390. return retval;
  391. }
  392. static u32 crypto4xx_put_sd_to_sdr(struct crypto4xx_device *dev)
  393. {
  394. unsigned long flags;
  395. spin_lock_irqsave(&dev->core_dev->lock, flags);
  396. if (dev->sdr_tail == dev->sdr_head) {
  397. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  398. return 0;
  399. }
  400. if (dev->sdr_tail != PPC4XX_LAST_SD)
  401. dev->sdr_tail++;
  402. else
  403. dev->sdr_tail = 0;
  404. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  405. return 0;
  406. }
  407. static inline struct ce_sd *crypto4xx_get_sdp(struct crypto4xx_device *dev,
  408. dma_addr_t *sd_dma, u32 idx)
  409. {
  410. *sd_dma = dev->sdr_pa + sizeof(struct ce_sd) * idx;
  411. return (struct ce_sd *)(dev->sdr + sizeof(struct ce_sd) * idx);
  412. }
  413. static u32 crypto4xx_fill_one_page(struct crypto4xx_device *dev,
  414. dma_addr_t *addr, u32 *length,
  415. u32 *idx, u32 *offset, u32 *nbytes)
  416. {
  417. u32 len;
  418. if (*length > dev->scatter_buffer_size) {
  419. memcpy(phys_to_virt(*addr),
  420. dev->scatter_buffer_va +
  421. *idx * dev->scatter_buffer_size + *offset,
  422. dev->scatter_buffer_size);
  423. *offset = 0;
  424. *length -= dev->scatter_buffer_size;
  425. *nbytes -= dev->scatter_buffer_size;
  426. if (*idx == PPC4XX_LAST_SD)
  427. *idx = 0;
  428. else
  429. (*idx)++;
  430. *addr = *addr + dev->scatter_buffer_size;
  431. return 1;
  432. } else if (*length < dev->scatter_buffer_size) {
  433. memcpy(phys_to_virt(*addr),
  434. dev->scatter_buffer_va +
  435. *idx * dev->scatter_buffer_size + *offset, *length);
  436. if ((*offset + *length) == dev->scatter_buffer_size) {
  437. if (*idx == PPC4XX_LAST_SD)
  438. *idx = 0;
  439. else
  440. (*idx)++;
  441. *nbytes -= *length;
  442. *offset = 0;
  443. } else {
  444. *nbytes -= *length;
  445. *offset += *length;
  446. }
  447. return 0;
  448. } else {
  449. len = (*nbytes <= dev->scatter_buffer_size) ?
  450. (*nbytes) : dev->scatter_buffer_size;
  451. memcpy(phys_to_virt(*addr),
  452. dev->scatter_buffer_va +
  453. *idx * dev->scatter_buffer_size + *offset,
  454. len);
  455. *offset = 0;
  456. *nbytes -= len;
  457. if (*idx == PPC4XX_LAST_SD)
  458. *idx = 0;
  459. else
  460. (*idx)++;
  461. return 0;
  462. }
  463. }
  464. static void crypto4xx_copy_pkt_to_dst(struct crypto4xx_device *dev,
  465. struct ce_pd *pd,
  466. struct pd_uinfo *pd_uinfo,
  467. u32 nbytes,
  468. struct scatterlist *dst)
  469. {
  470. dma_addr_t addr;
  471. u32 this_sd;
  472. u32 offset;
  473. u32 len;
  474. u32 i;
  475. u32 sg_len;
  476. struct scatterlist *sg;
  477. this_sd = pd_uinfo->first_sd;
  478. offset = 0;
  479. i = 0;
  480. while (nbytes) {
  481. sg = &dst[i];
  482. sg_len = sg->length;
  483. addr = dma_map_page(dev->core_dev->device, sg_page(sg),
  484. sg->offset, sg->length, DMA_TO_DEVICE);
  485. if (offset == 0) {
  486. len = (nbytes <= sg->length) ? nbytes : sg->length;
  487. while (crypto4xx_fill_one_page(dev, &addr, &len,
  488. &this_sd, &offset, &nbytes))
  489. ;
  490. if (!nbytes)
  491. return;
  492. i++;
  493. } else {
  494. len = (nbytes <= (dev->scatter_buffer_size - offset)) ?
  495. nbytes : (dev->scatter_buffer_size - offset);
  496. len = (sg->length < len) ? sg->length : len;
  497. while (crypto4xx_fill_one_page(dev, &addr, &len,
  498. &this_sd, &offset, &nbytes))
  499. ;
  500. if (!nbytes)
  501. return;
  502. sg_len -= len;
  503. if (sg_len) {
  504. addr += len;
  505. while (crypto4xx_fill_one_page(dev, &addr,
  506. &sg_len, &this_sd, &offset, &nbytes))
  507. ;
  508. }
  509. i++;
  510. }
  511. }
  512. }
  513. static u32 crypto4xx_copy_digest_to_dst(struct pd_uinfo *pd_uinfo,
  514. struct crypto4xx_ctx *ctx)
  515. {
  516. struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  517. struct sa_state_record *state_record =
  518. (struct sa_state_record *) pd_uinfo->sr_va;
  519. if (sa->sa_command_0.bf.hash_alg == SA_HASH_ALG_SHA1) {
  520. memcpy((void *) pd_uinfo->dest_va, state_record->save_digest,
  521. SA_HASH_ALG_SHA1_DIGEST_SIZE);
  522. }
  523. return 0;
  524. }
  525. static void crypto4xx_ret_sg_desc(struct crypto4xx_device *dev,
  526. struct pd_uinfo *pd_uinfo)
  527. {
  528. int i;
  529. if (pd_uinfo->num_gd) {
  530. for (i = 0; i < pd_uinfo->num_gd; i++)
  531. crypto4xx_put_gd_to_gdr(dev);
  532. pd_uinfo->first_gd = 0xffffffff;
  533. pd_uinfo->num_gd = 0;
  534. }
  535. if (pd_uinfo->num_sd) {
  536. for (i = 0; i < pd_uinfo->num_sd; i++)
  537. crypto4xx_put_sd_to_sdr(dev);
  538. pd_uinfo->first_sd = 0xffffffff;
  539. pd_uinfo->num_sd = 0;
  540. }
  541. }
  542. static u32 crypto4xx_ablkcipher_done(struct crypto4xx_device *dev,
  543. struct pd_uinfo *pd_uinfo,
  544. struct ce_pd *pd)
  545. {
  546. struct crypto4xx_ctx *ctx;
  547. struct ablkcipher_request *ablk_req;
  548. struct scatterlist *dst;
  549. dma_addr_t addr;
  550. ablk_req = ablkcipher_request_cast(pd_uinfo->async_req);
  551. ctx = crypto_tfm_ctx(ablk_req->base.tfm);
  552. if (pd_uinfo->using_sd) {
  553. crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo, ablk_req->nbytes,
  554. ablk_req->dst);
  555. } else {
  556. dst = pd_uinfo->dest_va;
  557. addr = dma_map_page(dev->core_dev->device, sg_page(dst),
  558. dst->offset, dst->length, DMA_FROM_DEVICE);
  559. }
  560. crypto4xx_ret_sg_desc(dev, pd_uinfo);
  561. if (ablk_req->base.complete != NULL)
  562. ablk_req->base.complete(&ablk_req->base, 0);
  563. return 0;
  564. }
  565. static u32 crypto4xx_ahash_done(struct crypto4xx_device *dev,
  566. struct pd_uinfo *pd_uinfo)
  567. {
  568. struct crypto4xx_ctx *ctx;
  569. struct ahash_request *ahash_req;
  570. ahash_req = ahash_request_cast(pd_uinfo->async_req);
  571. ctx = crypto_tfm_ctx(ahash_req->base.tfm);
  572. crypto4xx_copy_digest_to_dst(pd_uinfo,
  573. crypto_tfm_ctx(ahash_req->base.tfm));
  574. crypto4xx_ret_sg_desc(dev, pd_uinfo);
  575. /* call user provided callback function x */
  576. if (ahash_req->base.complete != NULL)
  577. ahash_req->base.complete(&ahash_req->base, 0);
  578. return 0;
  579. }
  580. static u32 crypto4xx_pd_done(struct crypto4xx_device *dev, u32 idx)
  581. {
  582. struct ce_pd *pd;
  583. struct pd_uinfo *pd_uinfo;
  584. pd = dev->pdr + sizeof(struct ce_pd)*idx;
  585. pd_uinfo = dev->pdr_uinfo + sizeof(struct pd_uinfo)*idx;
  586. if (crypto_tfm_alg_type(pd_uinfo->async_req->tfm) ==
  587. CRYPTO_ALG_TYPE_ABLKCIPHER)
  588. return crypto4xx_ablkcipher_done(dev, pd_uinfo, pd);
  589. else
  590. return crypto4xx_ahash_done(dev, pd_uinfo);
  591. }
  592. /**
  593. * Note: Only use this function to copy items that is word aligned.
  594. */
  595. void crypto4xx_memcpy_le(unsigned int *dst,
  596. const unsigned char *buf,
  597. int len)
  598. {
  599. u8 *tmp;
  600. for (; len >= 4; buf += 4, len -= 4)
  601. *dst++ = cpu_to_le32(*(unsigned int *) buf);
  602. tmp = (u8 *)dst;
  603. switch (len) {
  604. case 3:
  605. *tmp++ = 0;
  606. *tmp++ = *(buf+2);
  607. *tmp++ = *(buf+1);
  608. *tmp++ = *buf;
  609. break;
  610. case 2:
  611. *tmp++ = 0;
  612. *tmp++ = 0;
  613. *tmp++ = *(buf+1);
  614. *tmp++ = *buf;
  615. break;
  616. case 1:
  617. *tmp++ = 0;
  618. *tmp++ = 0;
  619. *tmp++ = 0;
  620. *tmp++ = *buf;
  621. break;
  622. default:
  623. break;
  624. }
  625. }
  626. static void crypto4xx_stop_all(struct crypto4xx_core_device *core_dev)
  627. {
  628. crypto4xx_destroy_pdr(core_dev->dev);
  629. crypto4xx_destroy_gdr(core_dev->dev);
  630. crypto4xx_destroy_sdr(core_dev->dev);
  631. dev_set_drvdata(core_dev->device, NULL);
  632. iounmap(core_dev->dev->ce_base);
  633. kfree(core_dev->dev);
  634. kfree(core_dev);
  635. }
  636. void crypto4xx_return_pd(struct crypto4xx_device *dev,
  637. u32 pd_entry, struct ce_pd *pd,
  638. struct pd_uinfo *pd_uinfo)
  639. {
  640. /* irq should be already disabled */
  641. dev->pdr_head = pd_entry;
  642. pd->pd_ctl.w = 0;
  643. pd->pd_ctl_len.w = 0;
  644. pd_uinfo->state = PD_ENTRY_FREE;
  645. }
  646. /*
  647. * derive number of elements in scatterlist
  648. * Shamlessly copy from talitos.c
  649. */
  650. static int get_sg_count(struct scatterlist *sg_list, int nbytes)
  651. {
  652. struct scatterlist *sg = sg_list;
  653. int sg_nents = 0;
  654. while (nbytes) {
  655. sg_nents++;
  656. if (sg->length > nbytes)
  657. break;
  658. nbytes -= sg->length;
  659. sg = sg_next(sg);
  660. }
  661. return sg_nents;
  662. }
  663. static u32 get_next_gd(u32 current)
  664. {
  665. if (current != PPC4XX_LAST_GD)
  666. return current + 1;
  667. else
  668. return 0;
  669. }
  670. static u32 get_next_sd(u32 current)
  671. {
  672. if (current != PPC4XX_LAST_SD)
  673. return current + 1;
  674. else
  675. return 0;
  676. }
  677. u32 crypto4xx_build_pd(struct crypto_async_request *req,
  678. struct crypto4xx_ctx *ctx,
  679. struct scatterlist *src,
  680. struct scatterlist *dst,
  681. unsigned int datalen,
  682. void *iv, u32 iv_len)
  683. {
  684. struct crypto4xx_device *dev = ctx->dev;
  685. dma_addr_t addr, pd_dma, sd_dma, gd_dma;
  686. struct dynamic_sa_ctl *sa;
  687. struct scatterlist *sg;
  688. struct ce_gd *gd;
  689. struct ce_pd *pd;
  690. u32 num_gd, num_sd;
  691. u32 fst_gd = 0xffffffff;
  692. u32 fst_sd = 0xffffffff;
  693. u32 pd_entry;
  694. unsigned long flags;
  695. struct pd_uinfo *pd_uinfo = NULL;
  696. unsigned int nbytes = datalen, idx;
  697. unsigned int ivlen = 0;
  698. u32 gd_idx = 0;
  699. /* figure how many gd is needed */
  700. num_gd = get_sg_count(src, datalen);
  701. if (num_gd == 1)
  702. num_gd = 0;
  703. /* figure how many sd is needed */
  704. if (sg_is_last(dst) || ctx->is_hash) {
  705. num_sd = 0;
  706. } else {
  707. if (datalen > PPC4XX_SD_BUFFER_SIZE) {
  708. num_sd = datalen / PPC4XX_SD_BUFFER_SIZE;
  709. if (datalen % PPC4XX_SD_BUFFER_SIZE)
  710. num_sd++;
  711. } else {
  712. num_sd = 1;
  713. }
  714. }
  715. /*
  716. * The follow section of code needs to be protected
  717. * The gather ring and scatter ring needs to be consecutive
  718. * In case of run out of any kind of descriptor, the descriptor
  719. * already got must be return the original place.
  720. */
  721. spin_lock_irqsave(&dev->core_dev->lock, flags);
  722. if (num_gd) {
  723. fst_gd = crypto4xx_get_n_gd(dev, num_gd);
  724. if (fst_gd == ERING_WAS_FULL) {
  725. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  726. return -EAGAIN;
  727. }
  728. }
  729. if (num_sd) {
  730. fst_sd = crypto4xx_get_n_sd(dev, num_sd);
  731. if (fst_sd == ERING_WAS_FULL) {
  732. if (num_gd)
  733. dev->gdr_head = fst_gd;
  734. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  735. return -EAGAIN;
  736. }
  737. }
  738. pd_entry = crypto4xx_get_pd_from_pdr_nolock(dev);
  739. if (pd_entry == ERING_WAS_FULL) {
  740. if (num_gd)
  741. dev->gdr_head = fst_gd;
  742. if (num_sd)
  743. dev->sdr_head = fst_sd;
  744. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  745. return -EAGAIN;
  746. }
  747. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  748. pd_uinfo = (struct pd_uinfo *)(dev->pdr_uinfo +
  749. sizeof(struct pd_uinfo) * pd_entry);
  750. pd = crypto4xx_get_pdp(dev, &pd_dma, pd_entry);
  751. pd_uinfo->async_req = req;
  752. pd_uinfo->num_gd = num_gd;
  753. pd_uinfo->num_sd = num_sd;
  754. if (iv_len || ctx->is_hash) {
  755. ivlen = iv_len;
  756. pd->sa = pd_uinfo->sa_pa;
  757. sa = (struct dynamic_sa_ctl *) pd_uinfo->sa_va;
  758. if (ctx->direction == DIR_INBOUND)
  759. memcpy(sa, ctx->sa_in, ctx->sa_len * 4);
  760. else
  761. memcpy(sa, ctx->sa_out, ctx->sa_len * 4);
  762. memcpy((void *) sa + ctx->offset_to_sr_ptr,
  763. &pd_uinfo->sr_pa, 4);
  764. if (iv_len)
  765. crypto4xx_memcpy_le(pd_uinfo->sr_va, iv, iv_len);
  766. } else {
  767. if (ctx->direction == DIR_INBOUND) {
  768. pd->sa = ctx->sa_in_dma_addr;
  769. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  770. } else {
  771. pd->sa = ctx->sa_out_dma_addr;
  772. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  773. }
  774. }
  775. pd->sa_len = ctx->sa_len;
  776. if (num_gd) {
  777. /* get first gd we are going to use */
  778. gd_idx = fst_gd;
  779. pd_uinfo->first_gd = fst_gd;
  780. pd_uinfo->num_gd = num_gd;
  781. gd = crypto4xx_get_gdp(dev, &gd_dma, gd_idx);
  782. pd->src = gd_dma;
  783. /* enable gather */
  784. sa->sa_command_0.bf.gather = 1;
  785. idx = 0;
  786. src = &src[0];
  787. /* walk the sg, and setup gather array */
  788. while (nbytes) {
  789. sg = &src[idx];
  790. addr = dma_map_page(dev->core_dev->device, sg_page(sg),
  791. sg->offset, sg->length, DMA_TO_DEVICE);
  792. gd->ptr = addr;
  793. gd->ctl_len.len = sg->length;
  794. gd->ctl_len.done = 0;
  795. gd->ctl_len.ready = 1;
  796. if (sg->length >= nbytes)
  797. break;
  798. nbytes -= sg->length;
  799. gd_idx = get_next_gd(gd_idx);
  800. gd = crypto4xx_get_gdp(dev, &gd_dma, gd_idx);
  801. idx++;
  802. }
  803. } else {
  804. pd->src = (u32)dma_map_page(dev->core_dev->device, sg_page(src),
  805. src->offset, src->length, DMA_TO_DEVICE);
  806. /*
  807. * Disable gather in sa command
  808. */
  809. sa->sa_command_0.bf.gather = 0;
  810. /*
  811. * Indicate gather array is not used
  812. */
  813. pd_uinfo->first_gd = 0xffffffff;
  814. pd_uinfo->num_gd = 0;
  815. }
  816. if (ctx->is_hash || sg_is_last(dst)) {
  817. /*
  818. * we know application give us dst a whole piece of memory
  819. * no need to use scatter ring.
  820. * In case of is_hash, the icv is always at end of src data.
  821. */
  822. pd_uinfo->using_sd = 0;
  823. pd_uinfo->first_sd = 0xffffffff;
  824. pd_uinfo->num_sd = 0;
  825. pd_uinfo->dest_va = dst;
  826. sa->sa_command_0.bf.scatter = 0;
  827. if (ctx->is_hash)
  828. pd->dest = virt_to_phys((void *)dst);
  829. else
  830. pd->dest = (u32)dma_map_page(dev->core_dev->device,
  831. sg_page(dst), dst->offset,
  832. dst->length, DMA_TO_DEVICE);
  833. } else {
  834. struct ce_sd *sd = NULL;
  835. u32 sd_idx = fst_sd;
  836. nbytes = datalen;
  837. sa->sa_command_0.bf.scatter = 1;
  838. pd_uinfo->using_sd = 1;
  839. pd_uinfo->dest_va = dst;
  840. pd_uinfo->first_sd = fst_sd;
  841. pd_uinfo->num_sd = num_sd;
  842. sd = crypto4xx_get_sdp(dev, &sd_dma, sd_idx);
  843. pd->dest = sd_dma;
  844. /* setup scatter descriptor */
  845. sd->ctl.done = 0;
  846. sd->ctl.rdy = 1;
  847. /* sd->ptr should be setup by sd_init routine*/
  848. idx = 0;
  849. if (nbytes >= PPC4XX_SD_BUFFER_SIZE)
  850. nbytes -= PPC4XX_SD_BUFFER_SIZE;
  851. else
  852. nbytes = 0;
  853. while (nbytes) {
  854. sd_idx = get_next_sd(sd_idx);
  855. sd = crypto4xx_get_sdp(dev, &sd_dma, sd_idx);
  856. /* setup scatter descriptor */
  857. sd->ctl.done = 0;
  858. sd->ctl.rdy = 1;
  859. if (nbytes >= PPC4XX_SD_BUFFER_SIZE)
  860. nbytes -= PPC4XX_SD_BUFFER_SIZE;
  861. else
  862. /*
  863. * SD entry can hold PPC4XX_SD_BUFFER_SIZE,
  864. * which is more than nbytes, so done.
  865. */
  866. nbytes = 0;
  867. }
  868. }
  869. sa->sa_command_1.bf.hash_crypto_offset = 0;
  870. pd->pd_ctl.w = ctx->pd_ctl;
  871. pd->pd_ctl_len.w = 0x00400000 | (ctx->bypass << 24) | datalen;
  872. pd_uinfo->state = PD_ENTRY_INUSE;
  873. wmb();
  874. /* write any value to push engine to read a pd */
  875. writel(1, dev->ce_base + CRYPTO4XX_INT_DESCR_RD);
  876. return -EINPROGRESS;
  877. }
  878. /**
  879. * Algorithm Registration Functions
  880. */
  881. static int crypto4xx_alg_init(struct crypto_tfm *tfm)
  882. {
  883. struct crypto_alg *alg = tfm->__crt_alg;
  884. struct crypto4xx_alg *amcc_alg = crypto_alg_to_crypto4xx_alg(alg);
  885. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  886. ctx->dev = amcc_alg->dev;
  887. ctx->sa_in = NULL;
  888. ctx->sa_out = NULL;
  889. ctx->sa_in_dma_addr = 0;
  890. ctx->sa_out_dma_addr = 0;
  891. ctx->sa_len = 0;
  892. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  893. default:
  894. tfm->crt_ablkcipher.reqsize = sizeof(struct crypto4xx_ctx);
  895. break;
  896. case CRYPTO_ALG_TYPE_AHASH:
  897. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  898. sizeof(struct crypto4xx_ctx));
  899. break;
  900. }
  901. return 0;
  902. }
  903. static void crypto4xx_alg_exit(struct crypto_tfm *tfm)
  904. {
  905. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  906. crypto4xx_free_sa(ctx);
  907. crypto4xx_free_state_record(ctx);
  908. }
  909. int crypto4xx_register_alg(struct crypto4xx_device *sec_dev,
  910. struct crypto4xx_alg_common *crypto_alg,
  911. int array_size)
  912. {
  913. struct crypto4xx_alg *alg;
  914. int i;
  915. int rc = 0;
  916. for (i = 0; i < array_size; i++) {
  917. alg = kzalloc(sizeof(struct crypto4xx_alg), GFP_KERNEL);
  918. if (!alg)
  919. return -ENOMEM;
  920. alg->alg = crypto_alg[i];
  921. alg->dev = sec_dev;
  922. switch (alg->alg.type) {
  923. case CRYPTO_ALG_TYPE_AHASH:
  924. rc = crypto_register_ahash(&alg->alg.u.hash);
  925. break;
  926. default:
  927. rc = crypto_register_alg(&alg->alg.u.cipher);
  928. break;
  929. }
  930. if (rc) {
  931. list_del(&alg->entry);
  932. kfree(alg);
  933. } else {
  934. list_add_tail(&alg->entry, &sec_dev->alg_list);
  935. }
  936. }
  937. return 0;
  938. }
  939. static void crypto4xx_unregister_alg(struct crypto4xx_device *sec_dev)
  940. {
  941. struct crypto4xx_alg *alg, *tmp;
  942. list_for_each_entry_safe(alg, tmp, &sec_dev->alg_list, entry) {
  943. list_del(&alg->entry);
  944. switch (alg->alg.type) {
  945. case CRYPTO_ALG_TYPE_AHASH:
  946. crypto_unregister_ahash(&alg->alg.u.hash);
  947. break;
  948. default:
  949. crypto_unregister_alg(&alg->alg.u.cipher);
  950. }
  951. kfree(alg);
  952. }
  953. }
  954. static void crypto4xx_bh_tasklet_cb(unsigned long data)
  955. {
  956. struct device *dev = (struct device *)data;
  957. struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
  958. struct pd_uinfo *pd_uinfo;
  959. struct ce_pd *pd;
  960. u32 tail;
  961. while (core_dev->dev->pdr_head != core_dev->dev->pdr_tail) {
  962. tail = core_dev->dev->pdr_tail;
  963. pd_uinfo = core_dev->dev->pdr_uinfo +
  964. sizeof(struct pd_uinfo)*tail;
  965. pd = core_dev->dev->pdr + sizeof(struct ce_pd) * tail;
  966. if ((pd_uinfo->state == PD_ENTRY_INUSE) &&
  967. pd->pd_ctl.bf.pe_done &&
  968. !pd->pd_ctl.bf.host_ready) {
  969. pd->pd_ctl.bf.pe_done = 0;
  970. crypto4xx_pd_done(core_dev->dev, tail);
  971. crypto4xx_put_pd_to_pdr(core_dev->dev, tail);
  972. pd_uinfo->state = PD_ENTRY_FREE;
  973. } else {
  974. /* if tail not done, break */
  975. break;
  976. }
  977. }
  978. }
  979. /**
  980. * Top Half of isr.
  981. */
  982. static irqreturn_t crypto4xx_ce_interrupt_handler(int irq, void *data)
  983. {
  984. struct device *dev = (struct device *)data;
  985. struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
  986. if (core_dev->dev->ce_base == 0)
  987. return 0;
  988. writel(PPC4XX_INTERRUPT_CLR,
  989. core_dev->dev->ce_base + CRYPTO4XX_INT_CLR);
  990. tasklet_schedule(&core_dev->tasklet);
  991. return IRQ_HANDLED;
  992. }
  993. /**
  994. * Supported Crypto Algorithms
  995. */
  996. struct crypto4xx_alg_common crypto4xx_alg[] = {
  997. /* Crypto AES modes */
  998. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER, .u.cipher = {
  999. .cra_name = "cbc(aes)",
  1000. .cra_driver_name = "cbc-aes-ppc4xx",
  1001. .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
  1002. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1003. .cra_blocksize = AES_BLOCK_SIZE,
  1004. .cra_ctxsize = sizeof(struct crypto4xx_ctx),
  1005. .cra_type = &crypto_ablkcipher_type,
  1006. .cra_init = crypto4xx_alg_init,
  1007. .cra_exit = crypto4xx_alg_exit,
  1008. .cra_module = THIS_MODULE,
  1009. .cra_u = {
  1010. .ablkcipher = {
  1011. .min_keysize = AES_MIN_KEY_SIZE,
  1012. .max_keysize = AES_MAX_KEY_SIZE,
  1013. .ivsize = AES_IV_SIZE,
  1014. .setkey = crypto4xx_setkey_aes_cbc,
  1015. .encrypt = crypto4xx_encrypt,
  1016. .decrypt = crypto4xx_decrypt,
  1017. }
  1018. }
  1019. }},
  1020. };
  1021. /**
  1022. * Module Initialization Routine
  1023. */
  1024. static int __init crypto4xx_probe(struct of_device *ofdev,
  1025. const struct of_device_id *match)
  1026. {
  1027. int rc;
  1028. struct resource res;
  1029. struct device *dev = &ofdev->dev;
  1030. struct crypto4xx_core_device *core_dev;
  1031. rc = of_address_to_resource(ofdev->node, 0, &res);
  1032. if (rc)
  1033. return -ENODEV;
  1034. if (of_find_compatible_node(NULL, NULL, "amcc,ppc460ex-crypto")) {
  1035. mtdcri(SDR0, PPC460EX_SDR0_SRST,
  1036. mfdcri(SDR0, PPC460EX_SDR0_SRST) | PPC460EX_CE_RESET);
  1037. mtdcri(SDR0, PPC460EX_SDR0_SRST,
  1038. mfdcri(SDR0, PPC460EX_SDR0_SRST) & ~PPC460EX_CE_RESET);
  1039. } else if (of_find_compatible_node(NULL, NULL,
  1040. "amcc,ppc405ex-crypto")) {
  1041. mtdcri(SDR0, PPC405EX_SDR0_SRST,
  1042. mfdcri(SDR0, PPC405EX_SDR0_SRST) | PPC405EX_CE_RESET);
  1043. mtdcri(SDR0, PPC405EX_SDR0_SRST,
  1044. mfdcri(SDR0, PPC405EX_SDR0_SRST) & ~PPC405EX_CE_RESET);
  1045. } else if (of_find_compatible_node(NULL, NULL,
  1046. "amcc,ppc460sx-crypto")) {
  1047. mtdcri(SDR0, PPC460SX_SDR0_SRST,
  1048. mfdcri(SDR0, PPC460SX_SDR0_SRST) | PPC460SX_CE_RESET);
  1049. mtdcri(SDR0, PPC460SX_SDR0_SRST,
  1050. mfdcri(SDR0, PPC460SX_SDR0_SRST) & ~PPC460SX_CE_RESET);
  1051. } else {
  1052. printk(KERN_ERR "Crypto Function Not supported!\n");
  1053. return -EINVAL;
  1054. }
  1055. core_dev = kzalloc(sizeof(struct crypto4xx_core_device), GFP_KERNEL);
  1056. if (!core_dev)
  1057. return -ENOMEM;
  1058. dev_set_drvdata(dev, core_dev);
  1059. core_dev->ofdev = ofdev;
  1060. core_dev->dev = kzalloc(sizeof(struct crypto4xx_device), GFP_KERNEL);
  1061. if (!core_dev->dev)
  1062. goto err_alloc_dev;
  1063. core_dev->dev->core_dev = core_dev;
  1064. core_dev->device = dev;
  1065. spin_lock_init(&core_dev->lock);
  1066. INIT_LIST_HEAD(&core_dev->dev->alg_list);
  1067. rc = crypto4xx_build_pdr(core_dev->dev);
  1068. if (rc)
  1069. goto err_build_pdr;
  1070. rc = crypto4xx_build_gdr(core_dev->dev);
  1071. if (rc)
  1072. goto err_build_gdr;
  1073. rc = crypto4xx_build_sdr(core_dev->dev);
  1074. if (rc)
  1075. goto err_build_sdr;
  1076. /* Init tasklet for bottom half processing */
  1077. tasklet_init(&core_dev->tasklet, crypto4xx_bh_tasklet_cb,
  1078. (unsigned long) dev);
  1079. /* Register for Crypto isr, Crypto Engine IRQ */
  1080. core_dev->irq = irq_of_parse_and_map(ofdev->node, 0);
  1081. rc = request_irq(core_dev->irq, crypto4xx_ce_interrupt_handler, 0,
  1082. core_dev->dev->name, dev);
  1083. if (rc)
  1084. goto err_request_irq;
  1085. core_dev->dev->ce_base = of_iomap(ofdev->node, 0);
  1086. if (!core_dev->dev->ce_base) {
  1087. dev_err(dev, "failed to of_iomap\n");
  1088. goto err_iomap;
  1089. }
  1090. /* need to setup pdr, rdr, gdr and sdr before this */
  1091. crypto4xx_hw_init(core_dev->dev);
  1092. /* Register security algorithms with Linux CryptoAPI */
  1093. rc = crypto4xx_register_alg(core_dev->dev, crypto4xx_alg,
  1094. ARRAY_SIZE(crypto4xx_alg));
  1095. if (rc)
  1096. goto err_start_dev;
  1097. return 0;
  1098. err_start_dev:
  1099. iounmap(core_dev->dev->ce_base);
  1100. err_iomap:
  1101. free_irq(core_dev->irq, dev);
  1102. irq_dispose_mapping(core_dev->irq);
  1103. tasklet_kill(&core_dev->tasklet);
  1104. err_request_irq:
  1105. crypto4xx_destroy_sdr(core_dev->dev);
  1106. err_build_sdr:
  1107. crypto4xx_destroy_gdr(core_dev->dev);
  1108. err_build_gdr:
  1109. crypto4xx_destroy_pdr(core_dev->dev);
  1110. err_build_pdr:
  1111. kfree(core_dev->dev);
  1112. err_alloc_dev:
  1113. kfree(core_dev);
  1114. return rc;
  1115. }
  1116. static int __exit crypto4xx_remove(struct of_device *ofdev)
  1117. {
  1118. struct device *dev = &ofdev->dev;
  1119. struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
  1120. free_irq(core_dev->irq, dev);
  1121. irq_dispose_mapping(core_dev->irq);
  1122. tasklet_kill(&core_dev->tasklet);
  1123. /* Un-register with Linux CryptoAPI */
  1124. crypto4xx_unregister_alg(core_dev->dev);
  1125. /* Free all allocated memory */
  1126. crypto4xx_stop_all(core_dev);
  1127. return 0;
  1128. }
  1129. static struct of_device_id crypto4xx_match[] = {
  1130. { .compatible = "amcc,ppc4xx-crypto",},
  1131. { },
  1132. };
  1133. static struct of_platform_driver crypto4xx_driver = {
  1134. .name = "crypto4xx",
  1135. .match_table = crypto4xx_match,
  1136. .probe = crypto4xx_probe,
  1137. .remove = crypto4xx_remove,
  1138. };
  1139. static int __init crypto4xx_init(void)
  1140. {
  1141. return of_register_platform_driver(&crypto4xx_driver);
  1142. }
  1143. static void __exit crypto4xx_exit(void)
  1144. {
  1145. of_unregister_platform_driver(&crypto4xx_driver);
  1146. }
  1147. module_init(crypto4xx_init);
  1148. module_exit(crypto4xx_exit);
  1149. MODULE_LICENSE("GPL");
  1150. MODULE_AUTHOR("James Hsiao <jhsiao@amcc.com>");
  1151. MODULE_DESCRIPTION("Driver for AMCC PPC4xx crypto accelerator");